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


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

Re: Matrices as operators

From Ray Koopman <koopman@sfu.ca>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Matrices as operators
Date 2011-11-27 09:17 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jasv7r$764$1@smc.vnet.net> (permalink)

Show all headers | View raw


----- Christopher Young <cy56@comcast.net> wrote:
> 
> On Nov 25, 2011, at 6:17 PM, Ray Koopman wrote:
> 
>> 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] }
> 
> 
> Checking out the Help for Map, I found that you could put tests
> in with an If[ ] that was sort of 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 If will be faster if you swap its last two arguments
and don't complement the Or:

op[arr_, t_] := Map[If[NumericQ@# || StringQ@#, #, #@t]&, arr, {-1}]

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


Thread

Re: Matrices as operators Ray Koopman <koopman@sfu.ca> - 2011-11-27 09:17 +0000

csiph-web