Message Boards Message Boards

0
|
3833 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

Thread works different when used with If

Posted 10 years ago

Hello,

I would like to use Thread to apply a function on arguments that are lists and arguments which are scalars.

The following behavior is what I am looking for:

blafun1[a_, b_, c_] := a + b + c
Thread[blafun1[a, b, {c1, c2, c3}], List, -1]
{a + b + c1, a + b + c2, a + b + c3}

however, when I use a function that makes use of an If like:

blafun2[a_, b_, c_] := If[a + b + c > 0, a + b + c, 2]

then I get the following result:

If[{a + b + c1, a + b + c2, a + b + c3} > 0, a + b + {c1, c2, c3}, 2]

why is that? And how can I achieve what I want to?

Best Regards, Mathias

POSTED BY: Mathias Breuss

Hi,

Not sure about Thread[], but see documentation for Listable

SetAttributes[blafun2, Listable] ;    
blafun2[a_, b_, c_] := If[a + b + c > 0, a + b + c, 2] ;
blafun2[a, b, {c1, c2, c3}]    
ClearAttributes[blafun2, Listable] ;
(* {If[a + b + c1 > 0, a + b + c1, 2], If[a + b + c2 > 0, a + b + c2, 2], If[a + b + c3 > 0, a + b + c3, 2]} *)

I.M.

POSTED BY: Ivan Morozov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract