If I assign something like this to variable Res:
Res=Solve[6-5x+x^2==0];
What will do the following line?
Res[[1,1,2]]
or
Res[[2,1,2]]
Let's examine the FullForm of Res to see what Res[[1,1,2]] means:
In[1]:= Res = Solve[6 - 5 x + x^2 == 0]
Out[1]= {{x -> 2}, {x -> 3}}
In[2]:= FullForm[Res]
Out[2]= List[List[Rule[x, 2]], List[Rule[x, 3]]]
Hence:
Res[[1]] = List[Rule[x,2]]
Res[[1,1]] = Rule[x,2]
Res[[1,1,2]] = 2
Related
I have a simple Mathematica code below where I first introduce a scalar function ϕ = ϕ[x,y,z] and then calculate the gradient of ϕ. Now, I would like to evaluate the Gradient at point P by substituting in proper values for x, y, z. Please assist me with last step with plugging values into x and y into gradient. See code Below:
ϕ = y^2 + z^2 - 4;
varlist = {x, y, z}
Delϕ = Table[D[ϕ, varlist[[i]]], {j, 1, 1}, {i, 1, 3}]
Delϕ // MatrixForm
P = {2, 1, Sqrt (3)}
Thanks
Assuming you meant y^2 + z^2 - 4 x
φ = y^2 + z^2 - 4 x;
varlist = {x, y, z};
g = D[φ, #] & /# varlist
{-4, 2 y, 2 z}
p = {2, 1, Sqrt[3]};
grad = g /. Thread[varlist -> p]
{-4, 2, 2 Sqrt[3]}
another approach is to make your derivative a function:
\[Phi] = y^2 + z^2 - 4 x;
varlist = {x, y, z};
Del\[Phi][{x_, y_, z_}] = Table[D[\[Phi], varlist[[i]]], {i, 1, 3}];
then you can simply do this:
P = {2, 1, Sqrt[3]};
Del\[Phi][P]
{-4, 2, 2 Sqrt[3]}
I want to evaluate f[x,y]=-4 x + x^2 - 4 y - y^2 at points (1,-2); (2,-3); (3,-2); (2,-1).
I tried using Outer but for some reason it does not give me actual values. Help.
Remember that Mathematica has a specific way of defining functions. In your case it would be f[x_,y_]:=-4 x + x^2 - 4 y - y^2. Then you could simply use f[1,-2] etc.
Perhaps consider using a 'pure' function. For example:
-4 #1 + #1^2 - 4*#2 - #2^2 & ### {{1, -2}, {2, -3}, {3, -2}, {2, -1}}
gives
{1, -1, 1, -1}
Here are some variations on the theme:
Clear[f]
f[{x_, y_}] := -4 x + x^2 - 4 y - y^2
points = {{1, -2}, {2, -3}, {3, -2}, {2, -1}};
Map[f, points]
{1, -1, 1, -1}
f[x_, y_] := -4 x + x^2 - 4 y - y^2
f[1, -2]
1
f = Function[{x, y}, -4 x + x^2 - 4 y - y^2];
f[1, -2]
1
You can use functions like Apply and Map to evaluate a function in a list of points, for example
f[x_, y_] := -4 x + x^2 - 4 y - y^2
pts = {{1, -2}, {2, -3}, {3, -2}, {2, -1}};
Apply[f, pts, {1}]
(* out: {1, -1, 1, -1} *)
or using ### as a short hand for Apply[ ...., {1}]
f ### pts
I defined 2 objects:
f=x^2
g=x->#1
Why does this:
f /. x -> #1 &[5]
give me the expected result:
25
But this:
f /. g &[5]
gives me:
#1^2
As if the #1 wasn't evaluated to 5.
Please help.
Function (short form &) has attribute HoldAll:
Attributes[Function]
{HoldAll, Protected}
Therefore g remains unevaluated. You can force it with Evaluate:
Evaluate[f /. g] &[5]
25
Evaluate will not work deeper in the expression; you cannot write f /. Evaluate[g] &
You can make it work by keeping the pure function components together.
f = x^2
g = x -> #1 &
f/. g[5]
25
To run it over a list form the function before mapping.
f = x^2
g = x -> #1
list = {1, 2, 3, 4, 5};
b = Block[{a}, Function[f /. a] /. a -> g]
Map[b, list]
{1, 4, 9, 16, 25}
And for the specific problem in the comments...
vars = {x, y};
f = x + y;
g = Table[vars[[i]] -> Slot[i], {i, 1, Length[vars]}];
b = Block[{a}, Function[f /. a] /. a -> g];
list = {{1, 2}, {3, 4}, {5, 6}};
Map[b[Sequence ## #] &, list]
{3, 7, 11}
With Mr. Wizard's answer this can become:
vars = {x, y};
f = x + y;
g = Table[vars[[i]] -> Slot[i], {i, 1, Length[vars]}];
list = {{1, 2}, {3, 4}, {5, 6}};
Map[Evaluate[f /. g] &[Sequence ## #] &, list]
{3, 7, 11}
Replace g=x->#1 with g=x->#1 & and f /. g &[5] with f/. g[5]
How can one add the number of powers of x in expressions like the following?
x^2f[x]g[x^3]
or
x^2g[x^4]
or
x^2g[x^2f[x^2]]
The counting is such that all these examples must return 6.
I was thinking of using Count with some pattern, but I didn't manage to construct a pattern for this.
Here's my quick hack - some of the behaviour (see the final example) might not be quite what you want:
SetAttributes[countPowers, Listable]
countPowers[expr_, symb_] := Module[{a},
Cases[{expr} /. symb -> symb^a // PowerExpand, symb^n_ :> n,
Infinity] /. a -> 1 // Total]
Then
In[3]:= countPowers[{x^2 f[x] g[x^3], x^2 g[x^4], x^2 g[x^2 f[x^2]]}, x]
Out[3]= {6, 6, 6}
and
In[4]:= countPowers[{x^(2 I) g[x^3], g[x, x^4],
x^2 g[E^(2 Pi I x) , f[x]^x]}, x]
Out[4]= {3 + 2 I, 5, 5}
Since you want to count x as an implicit power of 1, you could use this:
powerCount[x_Symbol][expr_] :=
Tr # Reap[PowerExpand[expr] /. {x^n_ :> Sow[n], x :> Sow[1]}][[2,1]]
powerCount[x] /#
{
x^2f[x]g[x^3],
x^2g[x^4],
x^2g[x^2f[x^2]]
}
{6, 6, 6}
Alternatively, this could be written without Sow and Reap if that makes it easier to read:
powerCount[x_Symbol][expr_] :=
Module[{t = 0}, PowerExpand[expr] /. {x^n_ :> (t += n), x :> t++}; t]
Either form can be made more terse using vanishing patterns, at the possible expense of clarity:
powerCount[x_Symbol][expr_] :=
Tr # Reap[PowerExpand[expr] /. x^n_ | x :> Sow[1 n]][[2, 1]]
In Mathematica, calling Solve, returns a list of rules, e.g.,
In[1]:= g = Solve[(x - 1) (x - 2) == 0, x]
Out[1]= {{x -> 1}, {x -> 2}}
How can I extract the numerical values 1 or 2 from g?
I tried using Part e.g., g[[1]] but it returns {x -> 1} and not 1.
Please advise.
To complement Belisarius' answer,
x /. g
with g = {{x -> 1}, {x -> 2}}, returns the list
{1, 2}
So to extract the first value, 1, we could use
First[x /. g]
Other alternatives are
x /. g[[1]]
(x /. g)[[1]] (* this is equivalent to the version using First *)
g[[1,1,2]]
x /. g[[1]]
Filler -> Thirty chars minimum