Hi,
How does one write Solve[exp==s, variable]/.s->b as a pure function without explicitly defining s? Something along the lines of: Solve[exp == #,variable]/.???->b
Hi,
How does one write Solve[exp==s, variable]/.s->b as a pure function without explicitly defining s? Something along the lines of: Solve[exp == #,variable]/.???->b
Not sure exactly what you are trying to do. Maybe this?
# /. Solve[#^2 == 4, #]
(* {-2, 2} *)
or
Solve[x^2 == #, x] /. # -> 4
(* {{x -> -2}, {x -> 2}} *)
Hi Rohit,
Neither of your examples. Perhaps it would have been better if I had written
Solve[exp==s, variable]/.s->variable
such as
Solve[Sqrt[a+b]==s, a]/.s->a
Giving
{{a → a^2 - b }}
My variables are arbitrary and I can replace them at will so long as I keep all the relationships the same; it allows me to greatly simplify my problem.
In[1]:= Function[Solve[Sqrt[a + b] == #, #]][a]
Out[1]= {{a -> 1/2 (1 - Sqrt[1 + 4 b])}, {a -> 1/2 (1 + Sqrt[1 + 4 b])}}
Hi Hans,
Thank you for your suggestion.
Your solution is a different way of writing Solve[Sqrt[a+b] == a, a] which is not what I’m trying to do but it may hold the necessary clue. I’ll try Solve[Sqrt[a+b] == #, a][a]… Unfortunately, that syntax is not accepted.
In my problem, both “a” and “s” are dummy variable names, and I am trying to avoid explicitly using “s” due to memory issues (it seems to be a small thing, but small things can add up fast). Once the equation is solved, I’m recycling the variable name “a” to conserve the number of names I’m using. Does that make more sense?
Unfortunately, that syntax is not accepted
Turn it into a function first.
Solve[Sqrt[a + b] == #, a] &[a]
(* {{a -> 1/2 (1 - Sqrt[1 + 4 b])}, {a -> 1/2 (1 + Sqrt[1 + 4 b])}} *)
But, I still don’t understand the problem you are trying to solve with “recycling variables”. If you want to free up memory why can’t you use Unset or Clear or Remove?
Hi Rohit,
Still not the correct answer as the output for
Solve[Sqrt[a+b]==??,a]???->a
must be
{{a->a^2-b}}.
What I’d like to do (avoid explicitly using s) may not be allowed, in which case I’ll just have to use Solve[Sqrt[a+b]==s, a]/.s->a (= {{a->s^2-b}}/.s->a = {{a->a^2-b}}).
Also, I am using Remove frequently but that still does not fix the memory problem as Mathematica seems to insists on remembering every calculation that my program (which is 664 KB of text) does in a way I don’t know how to prevent. Everything is purely symbolic and currently written procedurally, with a lot of modules (defined with := , as = appears not to work correctly) and very large paralleltables (I’ve tried breaking the tables up but that causes other problems). I’m a novice at writing code using the “pure function” formate, which various sources say uses less memory and is faster, but that is the direction I’d like to move to.
In the end, it is should not be necessary for others to understand why I wish to do what I wish to do in order to answer this question.
Thank you.
In the end, it is should not be necessary for others to understand why
I wish to do what I wish to do in order to answer this question.
It might be necessary. XY problem
While that maybe true in general, let me worry about other underlining issues for the time being because answering this particular question also helps me understand what can and can not be done using pure functions in Mathematica. Right now, my principle goal is to get code that produces my desired results, followed by learning more programming strategies, followed by the efficiency of the code. If it helps, the topology of the space I’m working in is important, not the metric (I’m free to use any yardstick I wish).
If the only way to get the result I desire is to explicitly declare a variable “s”, please say so.
And Rohit, I’m trying to be polite but it is getting difficult as I find it extremely offensive when people try to second guess me. I appreciate the help but please answer the question asked.
In[1]:= Function[Solve[Sqrt[a + b] == #, a]][#] /. # → a
Out[1]= {{a -> a^2 - b}}
The syntax coloring will look a bit odd when you try it out, but it works.
Hans,
Great and thank you very much.