Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 4 down vote accepted

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}

share|improve this answer

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}} *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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