I'm also not sure whether I fully understand what you are after. But whenever one is using functions more in a mathematical sense than in a programming one, I think it is worth thinking about using pure functions to represent them. Here is a list of pure functions which each does a polynomial mapping as you describe:
polynomials={
Function[{x, y}, {x + y^2, y - x}],
Function[{x, y}, {x - y^2, y + x}]
}
you can now almost do what you want:
polynomials[[1]][x,y]
polynomials[[1]][x,y]
only the composition is syntactically somewhat complicated (because you can't use the pattern matcher to unpack/destructure the list arguments for Function
s, so we have to use @@
/ Apply
):
polynomials[[1]] @@ polynomials[[2]][x, y]
but fortunately it is simple enough to provide a helper function which adds some syntactic sugar so handling the entries in the list is less involved:
poly[i_Integer][x_, y_] := polynomials[[i]][x, y]
poly[i_Integer][{x_, y_}] := polynomials[[i]][x, y]
now you can use the various entries like this:
poly[1][x, y]
poly[2][x, y]
and composition is also easier:
poly[1][poly[2][{x, y}]]
And you can also use Composition
, as suggested in other comments and answers:
(Composition @@ {poly[1],poly[2],poly[1]})[x, y]
Depending on what the entries in polynomials
actually are it might make sense to turn that list into an Association
which then would allow to access the entries by non-integer keys which might make your code more readable and less error prone...
s[s[x,y]]
doesn't return output other thans[{x + y^2, -x + y}]
- is that expected, or did you want{x + y^2 + (-x + y)^2, -2 x + y - y^2}
as the output? $\endgroup$s[{x_, y_}] := (* stuff *)
would be a way to go. $\endgroup${s1, s2, s3}
- what are you looking to return? Something like ` {s1[ s2[ s3[x,y] ] ], s2[ s3[ x,y] ], s3[ x,y] }`? $\endgroup$