Message Boards Message Boards

0
|
6558 Views
|
7 Replies
|
4 Total Likes
View groups...
Share
Share this post:

[?] Define a function with 4 arguments (integer, real, rational, complex)?

Posted 7 years ago

I have been trying to find a function which takes as its arguments , one integer, one approximate real number, one rationale number and one complex number. The order in which the arguments are specified is immaterial. The function produces the list exactly in the order in which the arguments are specified but no two arguments are of the same type. I have tried to use Orderless and Cases as Mathematica functions but have not succeeded. I would like help in this.

ClearAll[f]

SetAttributes[f, Orderless]

f[n_Integer, r_Real, y_Rational, z_Complex] := {n, r, y, z}

f[2.5, 12, 11/5, 5 + I]

{12, 2.5, 11/5, 5 + I}

(* This does not work because the order in which the list appears is \
not the same order in which the arguments in the function appear *)

(* The following is fine *)

f[2, 12, 11/5, 5 + I]

f[2, 11/5, 5 + I, 12]

f[2, 3/4, 11/5, 5 + I]

f[3/4, 2, 11/5, 5 + I]
7 Replies

Dear Sander Huisman

Many thanks. It is very good of you to have taken the time to do this. Regards

Mohammad

Essentially my question is as follow: Define a function that takes four arguments: an integer, an approximate real number, a rational number, and a complex number. However, the order in which arguments are specified is not important. In other words, the function should accept an integer, an approximate real number, a rational number, or a complex number as an argument at any position, but no two arguments can be of the same type. The function returns a list of the arguments in the same order as they appear in the function. Here are some examples:

h[3 + 5 I,7/3, 5.23, 11] {3 + 5 I, 7/3 , 5.23, 11}

h[11, 7/3, 3 + 5 I, 5.23] {11, 7/3 , 3+ 5 I, 5.23}

h[2 , 3, 4/3, 5.5] h[2, 3, 4/3, 5.5G]

h[11/3 , 5+ 2 I, 5.2, 3 + I] h[11/3 , 5+ 2 I, 5.2, 3 + I]

I have tried the following the first requirement is fulfilled but the second requirement i.e. no two arguments can be of the same type does not work

In[548]:= ClearAll[f, g, h, exp2, exp3]

In[549]:= SetAttributes[f, Orderless]

In[550]:= g[a_, b_, c_, d_] := Hold[{a, b, c, d}];

In[526]:= f[n_Integer, r_Real, y_Rational, z_Complex] := g[n, r, y, z];

In[551]:= h[a_, b_, c_, d_] := ReleaseHold[g[a, b, c, d]]


In[552]:= h[5.3, 3/4, 7 + I, 5]

Out[552]= {5.3, 3/4, 7 + I, 5}

In[553]:= h[5.3, 6, 7 + I, 5]

Out[553]= {5.3, 6, 7 + I, 5}

Ok, this is a lot better description, you should've posted this as your opening post...

I guess you should do it like this:

ClearAll[f]
f[n__] := {n} /; (Sort[Head /@ {n}] === {Complex, Integer, Rational, Real})

It is not the nicest way of doing it, but it will work. The requirements are also unusual I would say... If you just want different types you can do:

ClearAll[f]
f[n__] := {n} /; (UnsameQ @@ (Head /@ {n}))
POSTED BY: Sander Huisman
f[2, 12, 11/5, 5 + I] 

does not work because those are two integers, not one approximate real number as you said in your opening post.

Furthermore give this thread a proper title and provide clear examples of the expected outcomes for several 'good' inputs, and 'bad' inputs.

POSTED BY: Sander Huisman

Maybe this does the sorting you want, using Cases:

myF[w_, x_, y_, z_] := 
 Flatten@Map[
   Cases[{w, x, y, z}, #] &, {_Integer, _Real, _Rational, _Complex}]
POSTED BY: Gianluca Gorni

Sorry. Doesn't work

ClearAll[f]
SetAttributes[f, Orderless]
f[w_Real, x_Rational, y_Complex, z_Integer] := {w, x, y, z}

f[3, 4.5, 1 + 2 I, 3/4]  (* works *)
f[4.5, 3, 3/4, 1 + 2 I]  (* works *)
f[3, 4.5, 1 + 2 I, 3]   (* should not and does not work *)
POSTED BY: Sander Huisman
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