Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #4229

Re: Pattern in immediate definition

From "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Pattern in immediate definition
Date 2011-08-09 11:21 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j1r588$fg4$1@smc.vnet.net> (permalink)
References <j1o6dl$4i4$1@smc.vnet.net>

Show all headers | View raw


On Mon, 08 Aug 2011 09:23:17 +0100, eLVa <elvadrias@gmail.com> wrote:

> Hi,
>
> I want to write more clearly the following definition :
> TestFunction[{{a_, b_, c_}, {d_, e_, f_}, {g_, h_, k_}}] =
> (* immediate definition with = *)
>  Module[{F, det},
>   F = {{a, b, c}, {d, e, f}, {g, h, k}};
>   det = Det[F];
>   Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
>   ]
>
> After that, I can pass any 3x3 matrix to the function and get the
> result directly replaced, without having to evaluate the derivatives
> again , i.e I don't want it to be written :
>
> TestFunction[F_] :=
> (* delayed definition with := *)
>  Module[{det},
>   det = Det[F];
>   Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
>   ]
>
> for it will compute everything every time I get to call the function
> (which will be inefficient since I will call it often).
>
> However, I find the trick with the temporary F that gets to be
> assigned the matrix of {{a,b,c},{d,e,f},{g,h,i}} ugly and potentially
> dangerous since it uses the value of the variables (a,b,...,k) if they
> are defined.
>
> I would like something close to :
> TestFunction[F_<....>] = Module[<...>] where in the first <..> I get
> to specify that the argument is a 3x3 matrix and so I can have access
> to F[[i,j]] (Mathematica complains about this part since F is
> obviously just a symbol and not a matrix). This way I just have to
> worry about F not being defined elsewhere. It will also be a cleaner
> definition in my opinion.
>
> Is that possible in any way ??
> Thanks
>

Generally it will be better to evaluate the definition explicitly if that  
is what is required rather than leaving it implicit and depending on the  
values of various globally defined symbols. Here is a straightforward and  
safe way to accomplish this while still having the benefit of using a  
delayed definition:

Block[{a, b, c, d, e, f, g, h, k, F, det},
  F = {{a, b, c}, {d, e, f}, {g, h, k}};
  det = Det[F];
  TestFunction[{{a_, b_, c_}, {d_, e_, f_}, {g_, h_, k_}}] :=
   Evaluate@Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
]

As you can see, this is nothing more than a re-ordering of your code with  
Module also being replaced by Block.

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Pattern in immediate definition eLVa <elvadrias@gmail.com> - 2011-08-08 08:23 +0000
  Re: Pattern in immediate definition "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-08-09 11:21 +0000
  Re: Pattern in immediate definition Peter Pein <petsie@dordos.net> - 2011-08-09 11:28 +0000
  Re: Pattern in immediate definition Simon <simonjtyler@gmail.com> - 2011-08-11 07:25 +0000

csiph-web