3
$\begingroup$

I'm using polynomial mappings such as

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

and playing with compositions of such mapping, e.g.

s[s[x,y]]

My problem is I need to generate a list of these mappings (or an array, or what have you, I'm not sure what's the best data structure in this case) inside a loop, and later to compose them. So I would like to do something like

s[[1]][s[[2]][x,y]]

the first element in the list of mappings, composed on the second element. Can someone help me with that ? Also, in case there's a better way to define my polynomial mappings, please let me know.

$\endgroup$
7
  • $\begingroup$ Your input s[s[x,y]] doesn't return output other than s[{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$
    – Jason B.
    Dec 15, 2015 at 8:55
  • 1
    $\begingroup$ s[{x_, y_}] := (* stuff *) would be a way to go. $\endgroup$ Dec 15, 2015 at 8:56
  • $\begingroup$ Thanks. That's actually what I meant. I'll correct it. $\endgroup$
    – Teddy
    Dec 15, 2015 at 9:01
  • $\begingroup$ @Teddy, So I don't really understand - you want to create a list of functions, that is easy enough. But then what are you doing with these compositions? Say the functions are {s1, s2, s3} - what are you looking to return? Something like ` {s1[ s2[ s3[x,y] ] ], s2[ s3[ x,y] ], s3[ x,y] }`? $\endgroup$
    – Jason B.
    Dec 15, 2015 at 9:25
  • $\begingroup$ @J.M. I'm trying to create approximations for a certain mapping, call it $A[x,y]$, using polynomial mappings. This is done by analysing various parts of $A$, and generating polynomial mappings accordingly. The desired approximation will be the composition of all those polynomial mappings. $\endgroup$
    – Teddy
    Dec 15, 2015 at 9:30

1 Answer 1

4
$\begingroup$

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 Functions, 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...

$\endgroup$

Your Answer

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

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