Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #6554 > unrolled thread
| Started by | Chris Young <cy56@comcast.net> |
|---|---|
| First post | 2011-11-20 10:34 +0000 |
| Last post | 2011-11-27 09:13 +0000 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Matrices as operators Chris Young <cy56@comcast.net> - 2011-11-20 10:34 +0000
Re: Matrices as operators Simon <simonjtyler@gmail.com> - 2011-11-21 09:29 +0000
Re: Matrices as operators Ray Koopman <koopman@sfu.ca> - 2011-11-22 10:39 +0000
Re: Matrices as operators Chris Young <cy56@comcast.net> - 2011-11-25 09:54 +0000
Re: Matrices as operators Ray Koopman <koopman@sfu.ca> - 2011-11-26 10:12 +0000
Re: Matrices as operators Chris Young <cy56@comcast.net> - 2011-11-27 09:13 +0000
| From | Chris Young <cy56@comcast.net> |
|---|---|
| Date | 2011-11-20 10:34 +0000 |
| Subject | Matrices as operators |
| Message-ID | <jaal4e$12d$1@smc.vnet.net> |
I'd like to be able to abbreviate matrices such as rotation matrices so
that I don't have to repeat the argument. This way I can pass in more
complicated arguments and it also shows the structure of the
transformation more clearly.
Through[( {
{Cos, -Sin},
{Sin, Cos}
} )[θ]]
will get me partway there:
Out: {{Cos, -Sin}[θ], {Sin, Cos}[θ]}
I have to apply Thread and Through again to finally get what I want:
In: Thread[Through[{{Cos, -Sin}[θ], {Sin, Cos}[θ]}]]
Out: {{Cos[θ], (-Sin)[θ]}, {Sin[θ], Cos[θ]}}
Is there a shortcut way to do this all in one step?
Thanks very much for any help.
Chris Young
cy56@comcast.net
[toc] | [next] | [standalone]
| From | Simon <simonjtyler@gmail.com> |
|---|---|
| Date | 2011-11-21 09:29 +0000 |
| Message-ID | <jad5mb$efk$1@smc.vnet.net> |
| In reply to | #6554 |
You could use a pure function:
{{Cos@#, -Sin@#}, {Sin@#, Cos@#}}&[i]
returns
{{Cos[i], -Sin[i]}, {Sin[i], Cos[i]}}
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-11-22 10:39 +0000 |
| Message-ID | <jafu4b$ro7$1@smc.vnet.net> |
| In reply to | #6554 |
On Nov 20, 2:34 am, Chris Young <c...@comcast.net> wrote:
> I'd like to be able to abbreviate matrices such as rotation matrices
> so that I don't have to repeat the argument. This way I can pass in
> more complicated arguments and it also shows the structure of the
> transformation more clearly.
>
> Through[( {
> {Cos, -Sin},
> {Sin, Cos}
> } )[=CE=B8]]
>
> will get me partway there:
>
> Out: {{Cos, -Sin}[=CE=B8], {Sin, Cos}[=CE=B8]}
>
> I have to apply Thread and Through again to finally get what I want:
>
> In: Thread[Through[{{Cos, -Sin}[=CE=B8], {Sin, Cos}[=CE=B8]}]]
>
> Out: {{Cos[=CE=B8], (-Sin)[=CE=B8]}, {Sin[=CE=B8], Cos[=CE=B8]}}
>
> Is there a shortcut way to do this all in one step?
>
> Thanks very much for any help.
>
> Chris Young
> c...@comcast.net
If you're worried about redundant calculations when the matrices
are bigger than 2 x 2 and the functions are more complicated than
Sin and Cos, try something like
R[t_] := {{#1,-#2},{#2,#1}}&[Cos@t,Sin@t]
[toc] | [prev] | [next] | [standalone]
| From | Chris Young <cy56@comcast.net> |
|---|---|
| Date | 2011-11-25 09:54 +0000 |
| Message-ID | <janol1$aqb$1@smc.vnet.net> |
| In reply to | #6608 |
On 2011-11-22 10:39:07 +0000, Ray Koopman said:
> If you're worried about redundant calculations when the matrices
> are bigger than 2 x 2 and the functions are more complicated than
> Sin and Cos, try something like
>
> R[t_] := {{#1,-#2},{#2,#1}}&[Cos@t,Sin@t]
> You could have the expression optimizer even further optimize that
>
> Experimental`OptimizeExpression[{{#1, -#2}, {#2, #1}} &[Cos@t, Sin@t]]
>
> (That is what happens in Compile)
>
> Oliver
These look useful for speeding up calculations, and also clarifying the
structure of matrices.
For what I was looking for though (applying a matrix of different
function names, such as "Sin" and "Cos", to the same parameter, such as
the angle theta) I think the following will work.
I got unflatten[ ] from the Help for Partition (in the Applications section).
I was hoping that Partition alone would work, but reconstructing the
original array is more complicated than I thought. Not sure exactly
what Fold[ ] is doing here. Would like to try running this through the
debugger.
At any rate, I think the op[ ] routine I made will apply any array
("tensors", not just rectangular matrices) to a parameter as an
operator.
Partition a flat list of elements into a multidimensional array with
specified dimensions.
(Take[{2, 3, 4, 5}, {-1, 2, -1} means take the elements from the last
through the second.)
unflatten[
list_,
{dims__?((IntegerQ[#] && Positive[#]) &)}] \
:=
Fold[
Partition, list, Take[{dims}, {-1, 2, -1}]
] \
/;
(Length[list] === Times[dims])
This will apply matrix mat as an operator to the parameter θ :
op[mat_, θ_] := unflatten[
Through[Flatten[mat][θ]], Dimensions[mat]
]
In[536]:=
op[( {
{Cos, -Sin},
{Sin, Cos}
} ), θ]
Out[536]=
{{Cos[θ], (-Sin)[θ]}, {Sin[θ], Cos[θ]}}
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-11-26 10:12 +0000 |
| Message-ID | <jaqe26$nhv$1@smc.vnet.net> |
| In reply to | #6746 |
This is simpler. Does it do what you want?
Note that 'mat' can be a ragged array.
Mapping at {-1} preserves its shape, whatever it may be.
In[1]:= op[mat_,t_] := Map[#@t&, mat, {-1}]
In[2]:= op[ { {Sin, Cos}, Tan, whatever }, t ]
Out[2]= { {Sin[t], Cos[t]}, Tan[t], whatever[t] }
On Nov 25, 1:54 am, Chris Young <cy56@comcast.net> wrote:
> On 2011-11-22 10:39:07 +0000, Ray Koopman said:
>
>> If you're worried about redundant calculations when the matrices
>> are bigger than 2 x 2 and the functions are more complicated than
>> Sin and Cos, try something like
>>
>> R[t_] := {{#1,-#2},{#2,#1}}&[Cos@t,Sin@t]
>> You could have the expression optimizer even further optimize that
>>
>> Experimental`OptimizeExpression[{{#1, -#2}, {#2, #1}} &[Cos@t, Sin@t]]
>>
>> (That is what happens in Compile)
>>
>> Oliver
>
> These look useful for speeding up calculations, and also clarifying the
> structure of matrices.
>
> For what I was looking for though (applying a matrix of different
> function names, such as "Sin" and "Cos", to the same parameter, such as
> the angle theta) I think the following will work.
>
> I got unflatten[ ] from the Help for Partition (in the Applications section).
> I was hoping that Partition alone would work, but reconstructing the
> original array is more complicated than I thought. Not sure exactly
> what Fold[ ] is doing here. Would like to try running this through the
> debugger.
>
> At any rate, I think the op[ ] routine I made will apply any array
> ("tensors", not just rectangular matrices) to a parameter as an
> operator.
>
> Partition a flat list of elements into a multidimensional array with
> specified dimensions.
> (Take[{2, 3, 4, 5}, {-1, 2, -1} means take the elements from the last
> through the second.)
>
> unflatten[
> list_,
> {dims__?((IntegerQ[#] && Positive[#]) &)}] \
> :=
> Fold[
> Partition, list, Take[{dims}, {-1, 2, -1}]
> ] \
> /;
> (Length[list] === Times[dims])
>
> This will apply matrix mat as an operator to the parameter =CE=B8 :
>
> op[mat_, =CE=B8_] := unflatten[
> Through[Flatten[mat][=CE=B8]], Dimensions[mat]
> ]
>
> In[536]:=
>
> op[( {
> {Cos, -Sin},
> {Sin, Cos}
> } ), =CE=B8]
>
> Out[536]=
>
> {{Cos[=CE=B8], (-Sin)[=CE=B8]}, {Sin[=CE=B8], Cos[=CE=B8]}}
[toc] | [prev] | [next] | [standalone]
| From | Chris Young <cy56@comcast.net> |
|---|---|
| Date | 2011-11-27 09:13 +0000 |
| Message-ID | <jasuv5$6ui$1@smc.vnet.net> |
| In reply to | #6749 |
Checking out the Help for Map, I found that you could put tests in with
an If[ ] that was acting as a pure function. I think the following
will always do what I want. Should be much faster than using While
loops, too.
op2[arr_, t_] :=
Map[
If[! (NumericQ[#] || StringQ[#]),
#[t],
#
] &,
arr,
{-1}
]
In[38]:= op2[{{Sin, 3 E, Cos}, Tan, {Cos, Cos[Cos], 1}, "string"}, t]
Out[38]= {{Sin[t], 3 E, Cos[t]}, Tan[t], {Cos[t], Cos[Cos[t]], 1}, "string"}
The {-1} tells Map to only apply the change to the ends of the
expression tree. The If[ ] filters out numbers and strings, which are
not valid function names.
Some more tests would have to be added if you wanted this to work for
indexed functions such as f[k][x].
Seems like this is probably the fastest way to accomplish what I want,
since it's all built-in. Amazing!
Chris Young
cy56@comcast.net
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web