1
$\begingroup$

I've been trying to use Solve with pure functions to receive function definition and then use it with Map , to obtain results from mapping function to some data.

Here is an example

(#2 /. Solve[25 == #1^2 + #2^2, #2]) //circle equation

When I run this, I receive "function" definition

out1 = {-Sqrt[25 - #1^2], Sqrt[25 - #1^2]}

If I add & in the end of out1, and then Map this function to some data, I obtain what I need.

{-Sqrt[25 - #1^2], Sqrt[25 - #1^2]} & /@ Range[1, 10, 1]

Now I'm trying done this in one step:

 (#2 /. Solve[25 == #1^2 + #2^2, #2])&/@Range[1,10,1]

But I see that it isn't working, I see this is because added ampersand.

And here is another example

(y /. Solve[#1 x + #2 y + #3 == 0, y] &)[3, 4, 5]

it produces:

out2= {1/4 (-5 - 3 x)} 

OK, now I want to treat with this like a function and x as variable. I can do something like that:

   {1/4 (-5 - 3 #1)} &

And it will work, I can Map it, but it isn't what I wish.

Trying to do it in one step:

(y /. Solve[#1 *#4 + #2 y + #3 == 0, y] &)[3, 4, 5] & // #4 is x

I can't get what I want in one step.

Can anyone show how to do it?

$\endgroup$
3
  • 2
    $\begingroup$ I don't understand what your question is ... $\endgroup$
    – Szabolcs
    Apr 21, 2016 at 10:58
  • 3
    $\begingroup$ Please don´t use // (Postfix) for comments. This is used for postfix notation. Use (* ... *) instead. $\endgroup$
    – Yves Klett
    Apr 21, 2016 at 10:58
  • 1
    $\begingroup$ Please reformulate the question a bit more concisely, like this: "I tried this code. I expected this output. I got that output instead." $\endgroup$
    – Szabolcs
    Apr 21, 2016 at 11:00

2 Answers 2

4
$\begingroup$

Possibly this can be made more elegant but forcing Evaluation and applying Function can achieve what you want I think along these lines...

Function@Evaluate[(#2 /. Solve[25 == #1^2 + #2^2, #2])] /@  Range[1, 10, 1]
$\endgroup$
1
  • $\begingroup$ Thanks! It works! Evaluate make kerned to eval Solve and replace, and then use map. It also works with second example, But Evaluate do not need in this case. (x~Function~(y /. Solve[#1 *x + #2 y + #3 == 0, y] &[3, 4, 5])) /@ Range[1, 10, 1] $\endgroup$
    – Gelios
    Apr 21, 2016 at 12:01
3
$\begingroup$

This achieves the same as the answer from Ymareth, but uses a different way:

With[{f = (#2 /. Solve[25 == #1^2 + #2^2, #2])}, f & /@ Range[1, 10, 1]]

It first runs Solve and then inserts the result as the function body to be used in the Map.

I'd say it's a matter of taste which to use.

$\endgroup$
1
  • $\begingroup$ Thanks, it's too interesting way to solve my problem. $\endgroup$
    – Gelios
    Apr 21, 2016 at 12:36

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.