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


Groups > comp.soft-sys.math.mathematica > #4395 > unrolled thread

Re: Basic questions on _Plus and recursion

Started byHeike Gramberg <heike.gramberg@gmail.com>
First post2011-08-14 12:21 +0000
Last post2011-08-18 07:27 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Basic questions on _Plus and recursion Heike Gramberg <heike.gramberg@gmail.com> - 2011-08-14 12:21 +0000
    Re: Basic questions on _Plus and recursion Peter Pein <petsie@dordos.net> - 2011-08-18 07:27 +0000

#4395 — Re: Basic questions on _Plus and recursion

FromHeike Gramberg <heike.gramberg@gmail.com>
Date2011-08-14 12:21 +0000
SubjectRe: Basic questions on _Plus and recursion
Message-ID<j28ek0$3el$1@smc.vnet.net>
For the second part of your question you could do something like

f[a___, b_ /; Scalar[b] == 1, c___] := b f[a, c]

example:

Scalar[a] = 1;
Scalar[c] = 1;
f[a, b, c]

output:

a c f[b]

Heike

On 13 Aug 2011, at 12:49, Ethan Dyer wrote:

> Hi. I'm sure these are common issues, but I haven't found them. Here 
are the two, unrelated questions. Thank you for the help.
>
> 1) I want to use _Plus for a function with arbitrary arguments. ie I 
want: f[a+b]=f[a]+f[b], f[a+b,c+d] to go to 
f[a,c]+f[a,d]+f[b,c]+f[b,d], ... Is there a standard way to do this?
>
> 2)Here I want to pull out scalars from a function. Something like 
this:
>
> f[x__] :=(Times@@Select[{x},Scalar[#]==1&]) 
(f@@Select[{x},Scalar[#]!=1&])
>
> Where i have defined a function Scalar to =1 for some symbols.
>
> for instance say I had defined Scalar[a]=1 then f[a,b,c]= a 
f[b,c].
>
> The above does not work, as it gives infinite recursion. Is there a 
way to do this properly?
>
> Thank you again. Sorry if these are too common.
>
> Ethan
>

[toc] | [next] | [standalone]


#4458

FromPeter Pein <petsie@dordos.net>
Date2011-08-18 07:27 +0000
Message-ID<j2ietk$blu$1@smc.vnet.net>
In reply to#4395
Hi,

To make things look more like Mathematica and to speed things up I would
recommend a non-recursive version of Heike's pattern matching function
(and I changed "Scalar" to ScalarQ retrning True|False):

g[a___, b_ ? ScalarQ, c___] :=
  Block[ {sc = Pick[#, ScalarQ @ #]&[{a, c}]},
    (b Times @@ sc) *
      (g @@ DeleteCases[{a, c}, Alternatives @@ Union[sc]])
  ]

You might want to have a look at
http://dl.dropbox.com/u/3030567/Mathematica/factor_scalars.nb
where you'll find a very simple comparison of f and g.


Regards,
  Peter


Am 14.08.2011 14:21, schrieb Heike Gramberg:
> For the second part of your question you could do something like
> 
> f[a___, b_ /; Scalar[b] == 1, c___] := b f[a, c]
> 
> example:
> 
> Scalar[a] = 1;
> Scalar[c] = 1;
> f[a, b, c]
> 
> output:
> 
> a c f[b]
> 
> Heike
> 
> On 13 Aug 2011, at 12:49, Ethan Dyer wrote:
> 
>> Hi. I'm sure these are common issues, but I haven't found them. Here 
> are the two, unrelated questions. Thank you for the help.
>>
>> 1) I want to use _Plus for a function with arbitrary arguments. ie I 
> want: f[a+b]=f[a]+f[b], f[a+b,c+d] to go to 
> f[a,c]+f[a,d]+f[b,c]+f[b,d], ... Is there a standard way to do this?
>>
>> 2)Here I want to pull out scalars from a function. Something like 
> this:
>>
>> f[x__] :=(Times@@Select[{x},Scalar[#]==1&]) 
> (f@@Select[{x},Scalar[#]!=1&])
>>
>> Where i have defined a function Scalar to =1 for some symbols.
>>
>> for instance say I had defined Scalar[a]=1 then f[a,b,c]= a 
> f[b,c].
>>
>> The above does not work, as it gives infinite recursion. Is there a 
> way to do this properly?
>>
>> Thank you again. Sorry if these are too common.
>>
>> Ethan
>>
> 
> 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web