1
$\begingroup$

This is probably a simple problem, but I'm having a lot of issues with it because I'm unfamiliar with Mathematica syntax.

I have a list

S = {{-1 + x + y}, {x^2 + y}, {x + 2y}}

I am trying to create a function f which returns a sorted list after substitution, I've tried variations of the following:

f[x_, y_] := Sort[Evaluate[Table[u, {u, S}]]];

but it simply returns the original list. Any help would be much appreciated!

$\endgroup$

2 Answers 2

4
$\begingroup$

I'm not sure if this is what you want because you don't tell us how your list of polynomials in x and y originated, nor do you tell us how want the final result to look. So I'll make some assumptions.

Assuming that you typed in the expression for S, there is no reason you can't make it a function.

S[x_, y_] := {{-1 + x + y}, {x^2 + y}, {x + 2 y}}

To see how to go on from here, try evaluating S for a particular pair of inputs

S[2, -2]

{{-1}, {2}, {-2}}

Assuming that you want a simple list of sorted values as your final result, you would flatten the above list and then sort it.

Sort @ Flatten @ %

{-2, -1, 2}

Nou you only have to define f to carry out what was done for one particular pair,

f[x_, y_] := Flatten @ Sort @ S[x, y]
f[2, -2]

{-2, -1, 2}

$\endgroup$
3
$\begingroup$

This is how you could do it:

S = {{-1 + x + y}, {x^2 + y}, {x + 2 y}}

(* ==> {{-1 + x + y}, {x^2 + y}, {x + 2 y}} *)

f[x1_, y1_] := Sort[S /. {x -> x1, y -> y1}];

f[1, 2]

(* ==> {{2}, {3}, {5}} *)

f[2, 1]

(* ==> {{2}, {4}, {5}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.