MathGroup Archive 2011

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

Search the Archive

Re: Pattern in immediate definition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120775] Re: Pattern in immediate definition
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Tue, 9 Aug 2011 07:22:36 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201108080820.EAA04601@smc.vnet.net>

You can do this by wrapping your entire definition with Block:

Clear[TestFunction];
Block[{a, b, c, d, e, f, g, h, k} ,
 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}]]]]

It is also possible to write a function generator which would generate for
you a function
definition  for arbitrary matrix dimension:

Clear[generateTestF];
generateTestF[fname_Symbol, dim_Integer] :=
  Module[{m = Array[Unique[] &, {dim, dim}], det},
   det = Det[m];
   fname[Map[Pattern[#, Blank[]] &, m, {2}]] =
    Array[D[det, m[[##]]] &, {dim, dim}]];

Here is an example of use:

Clear[test];
generateTestF[test,3];

?test
Global`test
test[{{$21_,$22_,$23_},{$24_,$25_,$26_},{$27_,$28_,$29_}}]={{-$26 $28+$25
$29,$26 $27-$24 $29,-$25 $27+$24 $28},{$23 $28-$22 $29,-$23 $27+$21 $29,$22
$27-$21 $28},{-$23 $25+$22 $26,$23 $24-$21 $26,-$22 $24+$21 $25}}

It is slightly less readable since the dummy symbols were used to construct
it,  but otherwise the same as you would define "manually" for a fixed
matrix size. A slight downside of this solution is that the dummy symbols
were generated and left in the workspace, so if you call it several times
you'll get lots of them hanging around. But they are harmless, and  if
needed, the solution can be modified to address this problem as well.

Regards,
Leonid




On Mon, Aug 8, 2011 at 12:20 PM, eLVa <elvadrias at 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
>
>
>




  • Prev by Date: Re: Pattern in immediate definition
  • Next by Date: Re: Random prices from FinancialData
  • Previous by thread: Re: Pattern in immediate definition
  • Next by thread: Re: Pattern in immediate definition