Print output from Solve in a custom format - wolfram-mathematica

Solve[{A + B = 3, 2 A + B = 5}, {A, B}]
evaluates to A = 2, B = 1.
How can I Print the result like this
W = 2 + 1 * Sinx

Study https://reference.wolfram.com/language/ref/ReplaceAll.html and then try
Print[W==A+B*Sinx/.Solve[{A+B==3, 2A+B==5}, {A,B}][[1]]]
which prints
W==2+Sinx
because Mathematica automatically simplifies 1*Sinx to Sinx

Related

How to draw an n sample from U(a,b) of numbers not close?

I need to draw an n sample from the uniform distribution on the interval [a,b] such that no two numbers are closer than d > 0. I can draw a sample and check this property and then throw it away and try again if not, but if n is large relative to b-a that could take a looong time. Is there a simple and nice algorithm to solve this problem? The numbers got to be uniformly distributed on [a,b], no deterministic setup.
This problem is equivalent to choosing n numbers greater than or equal to d and whose sum is equal to b - a.
There will be some solution provided that n * d <= b - a. We can write a recursive algorithm that looks for one:
b - a - X < (n - 1) * D
X > b - a - (n - 1) * d
FindSpacedSample(n, d, a, b)
1. if n * d > b - a then return "no solution"
2. avail = [d, b - a - (n - 1) * d]
3. guess = random(avail)
4. print(guess)
5. FindSpacedSample(n - 1, d, a + guess, b)
Example: n = 5, a = 0, b = 10, d = 1, assuming real numbers
FindSpacedSample(5, 1, 0, 10)
5 * 1 >? b - a? no
avail = [1, 10 - 0 - 4 * 1] = [1, 6]
guess = random(avail) = 2 (for the sake of argument)
print(2)
FindSpacedSample(4, 1, 2, 10)
4 * 1 >? 10 - 2? no
avail = [1, 10 - 2 - 3 * 1] = [1, 5]
guess = random(avail) = 4 (for the sake of argument)
print(4)
FindSpacedSample(3, 1, 6, 10)
3 * 1 >? 10 - 6? no
avail = [1, 10 - 6 - 2 * 1] = [1, 2]
guess = random(avail) = 1 (for the sake of argument)
print(1)
FindSpacedSample(2, 1, 7, 10)
2 * 1 >? 10 - 7? no
avail = [1, 10 - 7 - 1 * 1] = [1, 2]
guess = random(avail) = 2 (for the sake of argument)
print(2)
FindSpacedSample(1, 1, 9, 10)
1 * 1 >? 10 - 9? no
avail = [1, 10 - 9 - 0 * 1] = [1, 1]
guess = 1
print(1)
We should also have stopping condition n = 0. Then we get the sequence of spaces 2, 4, 1, 2, 1; we see these sum to ten; and we can get the values as follows:
point1 = 2 = 2
point2 = 2 + 4 = 6
point3 = 2 + 4 + 1 = 7
point4 = 2 + 4 + 1 + 2 = 9
point5 = 2 + 4 + 1 + 2 + 1 = 10
Now, there are a couple of ways in which this result is less than totally uniform:
the first number will never be less than d
earlier numbers tend to be spaced further apart
We can fix these by:
shuffling the spacings before converting to points
subtracting from each point some random value from [0, point1 - a].
So, if we shuffled 2, 4, 1, 2, 1 to 4, 1, 1, 2, 2 we'd get points 4, 5, 6, 8, 10; and if we subtracted 3 from each one (taken randomly between 0 and 4) we'd get 1, 2, 3, 5, 7.
Does this give you a uniform distribution over the set of all possible solutions? I'd be surprised if it did, but I'd also be surprised if what this does give you differs from that truly uniform distribution to an appreciable degree.

Array equation explanation

I would appreciate if someone could explain why the output of this code:
*a, b = [1, 2, 3, 4]
a[b-2] + b
is 7. Could anyone please break it down line by line so I understand what's going on here? How does this become 7?
To break anything down line by line one could use REPL:
*a, b = [1, 2, 3, 4]
#⇒ [1, 2, 3, 4]
a
#⇒ [1, 2, 3]
b
#⇒ 4
Using splat operator, we have decomposed the original array to new array and the single value. Now everything is crystal clear: a[b-2] which is a[2], which is in turn 3 (check a array.) and b is still 4.
3 + 4
#⇒ 7
when you assign array like this,
*a, b = [1, 2, 3, 4]
then it will assign
a = [1,2,3]
and b = 4
Now when you tried to print a[b-2] + b
a[b - 2] = 3
b = 4
3 + 4 = 7
for more understanding use rails console and run line by line.
*a, b = [1, 2, 3, 4]
a
=> [1, 2, 3]
b
=> 4
a[b-2] // b-2 = 2 find value at index of 2 in a, like a[2] is 3
=> 3
a[b-2]+b
=> 7
*a, b = [1, 2, 3, 4] it means a = [1,2,3] and b = 4 when you do a[b-2] + b it will be
+-----------------------+
a[b-2] + b | a[2] |
a[4-2] + 4 | | |
a[2] + 4 | a[1, 2, 3] |
3 + 4 | 0 1 2 -> index |
= 7 +-----------------------+
By using the splat operator on a we are basically letting a to scoop up all the other numbers that b does not need. So b takes one number from the array and a takes everything that is left.
b = 4
a = [1,2,3]
Now when we do this:
a[b-2] + b
It translates into:
a[4-2] + 4
a[2] + 4
Now we check what number is in position 2 in array a.
3 + 4 = 7

How do I take derivatives in Wolfram Mathematica?

I am trying to learn a little bit about Wolfram Mathematica.
I want to define a symbolic function
where x is a vector, g is a function that takes a vector and returns a vector and h is a function that takes a vector and returns a scalar.
I don't want to commit to specific g and h, I just want to have a symbolic representation for them.
I would like to get a symbolic form for the third order derivatives (which would be a tensor) -- is there a way to do that in Wolfram Mathematica?
EDIT: I should mention, A and C are matrices, and b and d are vectors.
Here is what I tried and didn't work:
Try this
f[x_] := x*E^x
and then this
f'[x]
returns this
E^x + E^x x
and this
f''[x]
returns this
2 E^x + E^x x
Three methods of notation, all producing the same result.
f[x_] := Sin[x] + x^2
D[f[x], x]
2 x + Cos[x]
f'[x]
2 x + Cos[x]
f''[x]
2 - Sin[x]
using an alternative form of definition of f
Clear[f]
f = Sin[x] + x^2
D[f, x]
2 x + Cos[x]
δx f
2 x + Cos[x]
δ{x,2} f
2 - Sin[x]
Note
δ{x,2} f is supposed to be the subscript form of D[f, {x, 2}] but web formatting is limited.
Scoping out matrix and vector dimensions, and using S instead of C since the latter is a protected (uppercase) symbol.
A = {{1, 2, 3}, {4, 5, 6}};
x = {2, 4, 8};
A.x
{34, 76}
b = {3, 5};
h = 3;
h (A.x + b)
{111, 243}
S = {{1, 2}, {3, 4}, {5, 6}};
S.(h (A.x + b))
{597, 1305, 2013}
d = {2, 4, 8};
g = 2;
g (S.(h (A.x + b)) + d)
{1198, 2618, 4042}
So compatible matrix and vector assumptions can be made. (It turns out the derivative result comes out the same without taking the trouble to make the assumptions.)
Clear[A, x, b, S, d]
$Assumptions = {
Element[A, Matrices[{m, n}]],
Element[x, Vectors[n]],
Element[b, Vectors[m]],
Element[S, Matrices[{n, m}]],
Element[d, Vectors[n]]};
f = g (S.(h (A.x + b)) + d);
D[f, x]
g S.(h A.1)
D[f, {x, 3}]
0
I'm not sure if these results are correct so if you find out do comment.

Mathematica FindRoot solve equation

I have a task to solve equation system with FindRoot:
a*x+b*y^2-c*x^2=a-b, a*x^2+b*y^2+b*x-cy=b+c , where a = 10, b = 10, c = 6;
I'm very(!) new to Mathematica and have one day to get to know it.
Any comments on how to solve that equation will be much appreciated!
Thanks!
This starts searching for root at x = 0 and y = 0
eq = {a*x + b*y^2 - c*x^2 == a - b, a*x^2 + b*y^2 + b*x - c*y == b + c}
param = {a -> 10, b -> 10, c -> 6}
result = FindRoot[eq /. param, {x, 0}, {y, 0}]
This only gives you 1 of the two Real solutions. Solve will give you both (and even some solutions with Complex numbers). To test it:
eq /. param /. result
This returns (True, True) so you know you've found the correct root.
To find the solution graphically, use ContourPlot:
ContourPlot[Evaluate[eq /. param], {x, -5, 5}, {y, -5, 5}]
Here is the version with Solve/NSolve
NSolve[{a*x + b*y^2 - c*x^2 == a - b,
a*x^2 + b*y^2 + b*x - c*y == b + c} /. {a -> 10, b -> 10, c -> 6},
{x, y}]
It will give the 4 solutions. If you use Solve instead of NSolve you will have a (large) closed form of each component of each solution.
If you need more digits for the solution, add at the end of the NSolve command the option WorkingPrecision->30 (or any other number of digits. These are quadratics, they can computed to any precision necessary).

how to store the data from each loop into an array or table form?

For[n = 1, n < 6, n = n + 1,
For[m = 1, m < 6, m = m + 1, abc = doc[[n]];
kk = doc[[m]];
v =vector[abc, kk];
vl = VectorLength[v]]]
I want to store the data from each loop into an array or table form. How can I do that?
Try using a Table instead of two For loops. It returns a list of lists of the results (a matrix basically)
Table[
abc = doc[[n]];
kk = doc[[m]];
v = vector[abc, kk];
vl = VectorLength[v], {n, 1, 5}, {m, 1, 5}]
It's not clear to me what data you want to save, but the general way to do this is to use Sow and Reap.
Reap[
For[n = 1, n < 6, n = n + 1, For[m = 1, m < 6, m = m + 1,
abc = doc[[n]];
kk = doc[[m]];
Sow[v = vector[abc, kk]];
vl = VectorLength[v]]]
][[2, 1]]
This saves every value of v = vector[abc, kk]. The Part extraction [[2, 1]] returns only this list.
If you want to save multiple data sets, you can use tags within Sow:
Reap[
For[n = 1, n < 6, n = n + 1, For[m = 1, m < 6, m = m + 1,
abc = doc[[n]];
kk = doc[[m]];
Sow[v = vector[abc, kk], "v"];
Sow[vl = VectorLength[v], "v1"]
]]
]
Here I omit the Part extraction. Output is in the from {body, {{data1, ...}, {data2, ...}}} where body is any output from the expression itself (Null in the case of For). Data sets appear in the order they were first sown to. You can get an explicit order of sets with another argument of Reap as follows:
Reap[
For[ ... ],
{"v1", "v"}
]
See the documentation for Reap for more options.

Resources