Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Alternative to defining 'operator' function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47587] Re: Alternative to defining 'operator' function?
  • From: "Peter Pein" <petsie at arcor.de>
  • Date: Fri, 16 Apr 2004 05:21:58 -0400 (EDT)
  • References: <c5leel$bit$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"Owen, HL (Hywel)" <H.L.Owen at dl.ac.uk> schrieb im Newsbeitrag
news:c5leel$bit$1 at smc.vnet.net...
> Hi folks,
>
> I often have programming problem where I'd like to calculate a set of dot
> products, e.g. applying a list of square matrices {R1,R2,R3...} to a
vector
> v to obtain:
>
> {R1.v,R2.R1.v,R3.R2.R1.v,...}
>
> or other functions like that.
>
> The method I've been using is to define an 'operator' function, e.g.
>
> DotOperator[M_] := Dot[M, #] &
>
> Then we have:
>
> In: DotOperator[R][v]
> Out: R.v
>
> as wanted, so that we can define a ComposeList as
>
> In: Rest[ComposeList[DotOperator[#] & /@ {R1, R2, R3}, v]]
> Out: {R1.v, R2.R1.v, R3.R2.R1.v}
>
> to obtain the result we want.
>
> Is there a simpler way than this that doesn't involve defining functions
> like DotOperator?
>
> Thanks,
>
> Hywel
>

Hi Hywel,

your method gives the same result as

Rest[FoldList[#2.#1&,v,{R1,R2,R3...}]]

but you should consider the deinition of

ComposeDot[mlist_, v_] :=
    Nest[ReleaseHold, Rest[FoldList[Hold[#2.#1] &, v, mlist]],
      Length[mlist]];

In[2]:=
ComposeDot[{M1, M2, M3}, v]
Out[2]=
{M1.v, M2.M1.v, M3.M2.M1.v}

this is orders of magnitudes faster and uses memory more economical (see the
attached notebook for details).

Peter

(***********************************************************************

                    Mathematica-Compatible Notebook

This notebook can be used on any computer system with Mathematica 4.0,
MathReader 4.0, or any compatible application. The data for the notebook
starts with the line containing stars above.

To get the notebook into a Mathematica-compatible application, do one of
the following:

* Save the data starting with the line of stars above into a file
  with a name ending in .nb, then open the file inside the application;

* Copy the data starting with the line of stars above to the
  clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode.  Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing the
word CacheID, otherwise Mathematica-compatible applications may try to
use invalid cache data.

For more information on notebooks and Mathematica-compatible
applications, contact Wolfram Research:
  web: http://www.wolfram.com
  email: info at wolfram.com
  phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from
Wolfram Research.
***********************************************************************)


(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[      5571,        212]*)
(*NotebookOutlinePosition[      6224,        235]*)
(*  CellTagsIndexPosition[      6180,        231]*)
(*WindowFrame->Normal*)



Notebook[{

Cell[CellGroupData[{
Cell[BoxData[{
    \(\(ComposeDot[mlist_, v_] :=
        Nest[ReleaseHold, Rest[FoldList[Hold[#2 . #1] &, v, mlist]],
          Length[mlist]];\)\), "\[IndentingNewLine]",
    \(ComposeDot[{M1, M2, M3}, v]\)}], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \({M1 . v, M2 . M1 . v, M3 . M2 . M1 . v}\)], "Output"]
}, Open  ]],

Cell["This looks complicated compared to", "Text",
  CellAutoOverwrite->False],

Cell[CellGroupData[{

Cell[BoxData[{
    \(\(FoldDot[mlist_, v_] :=
        Rest[FoldList[#2 . #1 &, v, mlist]];\)\), "\[IndentingNewLine]",
    \(FoldDot[{M1, M2, M3}, v]\)}], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \({M1 . v, M2 . M1 . v, M3 . M2 . M1 . v}\)], "Output"]
}, Open  ]],

Cell["\[TripleDot]but it has got two advantages", "Text",
  CellAutoOverwrite->False],

Cell[BoxData[
    \(R\_i_ :=
      Block[{}, SeedRandom[i]; Table[Random[], {i + 1}, {i}]]\)], "Input",
  CellAutoOverwrite->False],

Cell[CellGroupData[{

Cell[BoxData[{
    \(\(mem = MaxMemoryUsed[];\)\), "\[IndentingNewLine]",
    \(First /@ {Timing[\(r1 =
              ComposeDot[\(R\_# &\) /@ Range[2, 8], {x,
                  y}];\)], \[IndentingNewLine]Timing[\(z1 =
              Expand[Last[r1]];\)]}\), "\[IndentingNewLine]",
    \(\(Print[MaxMemoryUsed[] - mem];\)\)}], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \({0.010000000000000009`\ Second,
      0.010000000000000009`\ Second}\)], "Output"],

Cell[BoxData[
    \(1432\)], "Print"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[{
    \(mem = MaxMemoryUsed[]; \), "\n",
    \(First /@ {Timing[r2 = FoldDot[\((R\_#1 &)\) /@ Range[2, 8], {x,
y}]; ],
        Timing[z2 = Expand[Last[r2]]; ]}\), "\n",
    \(Print[MaxMemoryUsed[] - mem]; \)}], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \({0.631`\ Second, 38.025`\ Second}\)], "Output"],

Cell[BoxData[
    \(17992\)], "Print"]
}, Open  ]],

Cell["Wow! \[HappySmiley]", "Text",
  CellAutoOverwrite->False],

Cell[CellGroupData[{

Cell[BoxData[
    \(z1 \[Equal] z2\)], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \(True\)], "Output"]
}, Open  ]],

Cell["\<\
\[TripleDot] of course.
    And the second advantage could be even more important:\
\>", "Text",
  CellAutoOverwrite->False],

Cell[CellGroupData[{

Cell[BoxData[{
    \(LeafCount /@ {r2, r1}\), "\[IndentingNewLine]",
    \(Quotient @@ %\)}], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \({2108798, 1121}\)], "Output"],

Cell[BoxData[
    \(1881\)], "Output"]
}, Open  ]],

Cell["or (same order of magnitude):", "Text",
  CellAutoOverwrite->False],

Cell[CellGroupData[{

Cell[BoxData[{
    \(ByteCount /@ {r2, r1}\), "\[IndentingNewLine]",
    \(Quotient @@ %\)}], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \({35916904, 18752}\)], "Output"],

Cell[BoxData[
    \(1915\)], "Output"]
}, Open  ]],

Cell["Your method (very similar to FoldDot):", "Text",
  CellAutoOverwrite->False],

Cell[CellGroupData[{

Cell[BoxData[{
    \(DotOperator[M_] := M . #1 &; \), "\n",
    \(mem = MaxMemoryUsed[]; \), "\n",
    \(First /@ {Timing[\(r3 =
              Rest[ComposeList[\((DotOperator[#1] &)\) /@ \((\(R\_#1 &\) /@
                        Range[2, 8])\), {x, y}]];\)],
        Timing[z3 = Expand[Last[r3]]; ]}\), "\n",
    \(\(Print[MaxMemoryUsed[] - mem];\)\)}], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \({0.6709999999999994`\ Second, 38.114000000000004`\ Second}\)],
"Output"],

Cell[BoxData[
    \(1464\)], "Print"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(z3 \[Equal] z2\)], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \(True\)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(ByteCount[r3]\)], "Input",
  CellAutoOverwrite->False],

Cell[BoxData[
    \(35916904\)], "Output"]
}, Open  ]]
},
FrontEndVersion->"4.0 for Microsoft Windows",
ScreenRectangle->{{0, 1024}, {0, 695}},
WindowSize->{897, 612},
WindowMargins->{{0, Automatic}, {Automatic, 0}}
]


(***********************************************************************
Cached data follows.  If you edit this Notebook file directly, not using
Mathematica, you must remove the line containing CacheID at the top of
the file.  The cache data will then be recreated when you save this file
from within Mathematica.
***********************************************************************)

(*CellTagsOutline
CellTagsIndex->{}
*)

(*CellTagsIndex
CellTagsIndex->{}
*)

(*NotebookFileOutline
Notebook[{

Cell[CellGroupData[{
Cell[1739, 51, 250, 5, 84, "Input"],
Cell[1992, 58, 73, 1, 38, "Output"]
}, Open  ]],
Cell[2080, 62, 78, 1, 45, "Text"],

Cell[CellGroupData[{
Cell[2183, 67, 193, 4, 61, "Input"],
Cell[2379, 73, 73, 1, 38, "Output"]
}, Open  ]],
Cell[2467, 77, 85, 1, 45, "Text"],
Cell[2555, 80, 132, 3, 40, "Input"],

Cell[CellGroupData[{
Cell[2712, 87, 368, 7, 107, "Input"],
Cell[3083, 96, 103, 2, 38, "Output"],
Cell[3189, 100, 37, 1, 28, "Print"]
}, Open  ]],

Cell[CellGroupData[{
Cell[3263, 106, 263, 5, 107, "Input"],
Cell[3529, 113, 67, 1, 38, "Output"],
Cell[3599, 116, 38, 1, 28, "Print"]
}, Open  ]],
Cell[3652, 120, 63, 1, 45, "Text"],

Cell[CellGroupData[{
Cell[3740, 125, 75, 2, 38, "Input"],
Cell[3818, 129, 38, 1, 38, "Output"]
}, Open  ]],
Cell[3871, 133, 134, 4, 70, "Text"],

Cell[CellGroupData[{
Cell[4030, 141, 131, 3, 61, "Input"],
Cell[4164, 146, 49, 1, 38, "Output"],
Cell[4216, 149, 38, 1, 38, "Output"]
}, Open  ]],
Cell[4269, 153, 73, 1, 45, "Text"],

Cell[CellGroupData[{
Cell[4367, 158, 131, 3, 61, "Input"],
Cell[4501, 163, 51, 1, 38, "Output"],
Cell[4555, 166, 38, 1, 38, "Output"]
}, Open  ]],
Cell[4608, 170, 82, 1, 45, "Text"],

Cell[CellGroupData[{
Cell[4715, 175, 396, 8, 176, "Input"],
Cell[5114, 185, 92, 1, 38, "Output"],
Cell[5209, 188, 37, 1, 28, "Print"]
}, Open  ]],

Cell[CellGroupData[{
Cell[5283, 194, 75, 2, 38, "Input"],
Cell[5361, 198, 38, 1, 38, "Output"]
}, Open  ]],

Cell[CellGroupData[{
Cell[5436, 204, 74, 2, 38, "Input"],
Cell[5513, 208, 42, 1, 38, "Output"]
}, Open  ]]
}
]
*)




(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)

-- 
Peter Pein, Berlin
if you want to write to me, start the subject with [




  • Prev by Date: Re: Critical memory leak with J/Link
  • Next by Date: RE: Count
  • Previous by thread: RE: Alternative to defining 'operator' function?
  • Next by thread: Re: Alternative to defining 'operator' function?