[Date Index]
[Thread Index]
[Author Index]
Re: What Happens to Garbage in Mathematica?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg44033] Re: What Happens to Garbage in Mathematica?
*From*: Olaf Rogalsky <olaf.rogalsky at theorie1.physik.uni-erlangen.de>
*Date*: Sat, 18 Oct 2003 03:12:33 -0400 (EDT)
*References*: <bm84s2$fug$1@smc.vnet.net> <bmg0nf$e9t$1@smc.vnet.net> <bmj2j5$pqs$1@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
Disclaimer: The following statements of mine don't realy express wisdom, but
my partial piece of knowledge, which is an other word for opinion. If anyone
of you has deeper insight, I would be delighted if you can enlighten me, too.
"Steven T. Hatton" wrote:
> [...] Currently, the only way I know to change
> the location of a primitive is to create a new one, and assign it to the
> original variable.
Yes, that is the way how functional programming languages work. In pure
functional languages one can't assign a new value to an existing
variable. Here, the term "variable" stands for: a specific place, where to
store values. But one can have several names (sequence of characters) for a
given variable. The connection between the name and the corresponding variable
is called binding. E.g., if one evaluates "i=1" in a new mathematica session, a
new binding between the name "i" and a newly created variable, which holds the
value "1", will be established. If one then evaluates "i=2", a new binding
between "i" and an *other*, *new* variable, which holds the value "2" will be
established.
Note 1: Out[1] still references the first variable, and therefore the allocated
memory for the first variable is not freed.
Note 2: Most functional languages are not puristic, which means, that they have
some way to reassign a variable. But I don't know, if Mathematica has.
Note 3: In garbage collected languages, memory allocation is very cheap, and even
when taking the price for garbage collection into account, they most often
outperform other memory management systems. Mathematica is reference
counted, which has a not so good performance reputation today.
> I'm still learning the syntax of Mathematica, so the following snippet is a
> bit hard to understnad. It's from the following file:
Mathematica's syntax is a pain in the ass, but I try to explain:
> RotateShape[ shape_, phi_, theta_, psi_ ] :=
Defines a new function
> Block[{rotmat = RotationMatrix3D[N[phi], N[theta], N[psi]]},
with a local binding for rotmat to the rotation matrix,
> shape /. {
which replaces all subexpressions in "shape", which match any of the following
rules:
> poly:Polygon[_] :> Map[(rotmat . #)&, poly, {2}],
This is the first rule, constisting of "pattern :> replacement". The pattern
matches any occurance of Polygon[<anything>], and gives this subexpression the name
"poly". Since the only valid agrument to the Polygon function is a list of coordinates,
for every math "poly" will be (temporary) bound to Polygon[<list of coordinates>].
The "&" in the expression "(rotmat . #)&" denotes, that the preceding expression defines
an unnamed function (a function, which has no name bound to it). In the definition of that
function, the "#" denotes the argument of the function. Since "." denotes the matrix/vector
product, the given function rotates any input vector by the given Euler angles.
The "Map" function applies this function to any coordinate of "poly" in turn and returns
a new Polygon[_] object, where the coordinates are replaced by the rotated ones.
> line:Line[_] :> Map[(rotmat . #)&, line, {2}],
> point:Point[_] :> Map[(rotmat . #)&, point,{1}] }
> ]
Two other, similar rules.
> Obviously it is something of a "switch" which branches to the appropriate
Not quite a switch, but similar.
> function (transformation rule?). It pulls the points out of the shape and
> transforms them using the rotation matrix. It would seem it places the
> results in a list with a head of the same type as the shape. What it
> clearly doesn't do is modify the 'members' or elements of the original
Yes, it returns a new copy. Again, consider the following code snipped:
"a=1; b=a; a=2". The first "=" does not assign "a" to the value "1", but bounds
"a" to a newly created variable which holds "1". The second "=" bounds "b" to the
same (identical) variable as "a". The third "=" establishes a new binding for
"a" to a new variable, which holds "2". The old variable of "a" is unchanged and
"b" still is bound to that variable.
>
> It would seem that if I were to assign the return value of RotateShape[] to
> lna like this: lna = RotateShape[ln, 2, 2, 2], I would create a dirty chunk
> of memory which will require housekeeping before it can be reused.
I don't understand you here.
> I've learned that Java3D tends to avoid allocating new memory in ways
> [...]
> be cheaper than applying the matrix multiplication on each point.
I have nothing new to say :-).
> I guess the essence of my original question is: how expensive is this
> garbage production and recycling?
There is an overhead to this, but don't worry too much. Mathematica is
an interpreted language, which does a lot of symbolic manipulations.
E.g. in your problem, the pattern matching propably is much more expensive
than the memory management.
Olaf Rogalsky
--
+-------------------------------------------------------------------+
I Dr. rer. nat. Olaf Rogalsky Institut fuer Theoretische Physik I
I Universitaet Erlangen-Nuernberg I
I Tel.: 09131 8528440 Staudtstr. 7 B3 I
I Fax.: 09131 8528444 D-91058 Erlangen I
| rogalsky at theorie1.physik.uni-erlangen.de I
+-------------------------------------------------------------------+
Prev by Date:
**Re: What Happens to Garbage in Mathematica?**
Next by Date:
**Re: Re: Concentric contours about the centroid, having the same length, and interior to an initial contour.**
Previous by thread:
**Re: What Happens to Garbage in Mathematica?**
Next by thread:
**Re: What Happens to Garbage in Mathematica?**
| |