MathGroup Archive 2010

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

Search the Archive

Re: Why do these not work?

  • To: mathgroup at
  • Subject: [mg109762] Re: Why do these not work?
  • From: "Alexander Elkins" <alexander_elkins at>
  • Date: Sun, 16 May 2010 05:56:41 -0400 (EDT)
  • References: <hse3nk$36b$>

(Re-posting - lost some how...)
"S. B. Gray" <stevebg at ROADRUNNER.COM> wrote in message
news:hse3nk$36b$1 at
> There is something about Mathematica which does not allow the first two
calls to
> work, and I don't know why. Can anyone give me some guidance?
> First, do I need all these things in the Module and is there a shorter
> way to "localize" them?
> plane3[p1_,p2_,p3_] :=
>   Module[{x1,y1,z1, x2,y2,z2, x3,y3,z3, iden},
>    {x1, y1, z1} = p1;
>    {x2, y2, z2} = p2;
>    {x3, y3, z3} = p3;
>    iden = 1/Det[{{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}}];
>     (* Or 1/Det[{p1,p2,p3}] *)
>     aa = iden*Det[{{ 1, y1, z1},{ 1, y2, z2},{ 1, y3, z3}}];
>     bb = iden*Det[{{x1,  1, z1},{x2,  1, z2},{x3,  1, z3}}];
>     cc = iden*Det[{{x1, y1,  1},{x2, y2,  1},{x3, y3,  1}}];
>     Return[{aa, bb, cc}];     (* Plane: aa*x + bb*y + cc*z = 1 *)
>    ]
The only thing truly wrong with the plane3 example is that it does not
require that each point be a list of length three.

Here is the simplest form to produce the result you appear to want, with no
intermediate variables, so there is nothing to localize along with an
assurance of only receiving input of the proper form, i.e. that each point
is a list of length three:


If a name for each of the elements of list making up each point is needed,
here is how to do that (notice that it is still not neccessary to have any
intermediate values to do this):


Mathematica evaluation only take place when the input signature matches,
working a bit like an implied replacement rule. The more specific the input
signatures are used first. For example:

In[3]:=f[1]:=Print["x is one"];f[x_Integer]:=Print["x is an integer"];
x is an integer
x is one

> This ReplaceAll gives the numeric answer I want:
> plane3[{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}] /.
>   {x1 -> 1, y1 -> 0, z1 -> 0,
>    x2 -> 0, y2 -> 1, z2 -> 0,
>    x3 -> 0, y3 -> 0, z3 -> 1}
> {1,1,1}

This works because plane3 returns a symbolic result containing symbols, all
of which have a replacement value.

> This ReplaceAll does not work at all:
> plane3[q1,q2,q3]/.{q1->{1,0,0},q2->{0,1,0},q3->{0,0,1}}

During evaluation of In[4]:= Set::shape: Lists {x1$456,y1$456,z1$456} and q1
are not the same shape. >>
During evaluation of In[4]:= Set::shape: Lists {x2$456,y2$456,z2$456} and q2
are not the same shape. >>
During evaluation of In[4]:= Set::shape: Lists {x3$456,y3$456,z3$456} and q3
are not the same shape. >>
(remaining output omitted)

The example plane3 does not require the input to have the proper form, i.e.
that each point must be a list of length three. As a result the plane3
example attempts access the elements which are not lists as lists when
evaluated in ths case. The example plane3s input pattern does not match so
that input remains unchanged when evaluated:


However after ReplaceAll (/.) changes plane3s[q1,q2,q3] into
plane3s[{1,0,0},{0,1,0},{0,0,1}] the result does match the input pattern for
plane3s when evaluated afterwards:


> This ReplaceAll gives a symbolic answer that I do not want:
> q1={x1,y1,z1}; q2={x2,y2,z2}; q3={x3,y3,z3};
> plane3[q1,q2,q3]/.{q1->{1,0,0},q2->{0,1,0},q3->{0,0,1}}

Both the plane3 and plane3s examples are evaluated before the replacement
occurs and therefore both return symbolic results which no longer contain
the symbols q1, q2 or q3. In other words, it is late to make the replacement
after the evaluation has already occured. By making sure the replacement
occurs first, the result is as expected:


Even this works since all the replacements occur first:


> What is the rule? I note in ReplaceAll x/.{{x->1},{x->3},{x->7}},
> but I don't want one at a time.

Using With[] avoids creating symbols in Global`* and performs the
replacement before any calculations occur:


> Thank you for any tips.
> Steve Gray

To create a package to handle Planes, Spheres, Cones, Cylinders and other 3D
objects you would not evaluate any of those objects as they appear.
Intersection, Draw3D, and their like would do that. Check out the Geometrica
application for Mathematica for hints. And sorry, I have no experience with

Alexander Elkins

  • Prev by Date: Re: How to write reports and books in Mathematica
  • Next by Date: Re: Latex, Mathematica, and journals
  • Previous by thread: Re: Why do these not work?
  • Next by thread: Different answer when running cell second time?