How to multiply symbolic with numeric?
Show older comments
Hi all'
assume that
w=randn(2,2)
a=sym('a',[2 2])
I need to get the answer of multiblication of (a .* w) with smae size [ 2 2] since, I multiplied them like (a .* w) i got this answer
ans =
[ -(4545034027795199*a1_1)/18014398509481984, -(8311781718565711*a1_2)/18014398509481984]
[ -(1390961227654289*a2_1)/1125899906842624, (5388115603050723*a2_2)/18014398509481984]
However, the random values of w is
w =
-0.2523 -0.4614
-1.2354 0.2991
a =
[ a1_1, a1_2]
[ a2_1, a2_2]
I don't belive this correct results however, I need the results like this [ -0.2523 * a1_1, -0.4614* a1_2
-1.2354 * a2_1, 0.2991 * a2_2]
thanks indeed
Accepted Answer
More Answers (1)
Walter Roberson
on 21 Dec 2020
w = round(sym(randn(2,2),'d'),4)
You are, by the way, not correct about what the value of w is. The actual values for w extend to about 15 significant decimal places, and you are getting confused because you have the default format in effect. I recommend that you use
format long g
and change your preferences to use that.
However, you defined your required output in terms of four decimal places, so I included a call to force four decimal places.
Note: symbolic floating point numbers give the strong impression that they are base 10 internally, but closer to the truth is that they use a base 2^30 representation, which is close enough to base 10^10 representation that it takes real effort to prove the difference.
13 Comments
Walter Roberson
on 21 Dec 2020
w23=sym('w23%d%d',[60,80]);
w23(11,1) => w23111
w23(1,11) => w23111
Are you sure you want to be generating duplicate symbol names?
w23=sym('w23%d_%d',[60,80]);
would not have any duplicates.
Ali Najem
on 21 Dec 2020
Ali Najem
on 21 Dec 2020
Walter Roberson
on 21 Dec 2020
MATLAB doesn't care much how many matrices you have, or how large they are, as long as you fit within available memory. There are theoretical limits, but you would need hundreds of petabytes of memory before you encountered the MATLAB limits.
You can, though, get performance issues depending on what you do with the matrices. For example det() of a dense 10 x 10 matrix takes more four minutes, and symbolic eigenvalues beyond about 4 x 4 takes quite long.
If you are taking a gradient with respect to a matrix of symbols, then the resulting array needs numel(expression) by numel(symbols), and calculating that can add up in time.
Ali Najem
on 21 Dec 2020
Ali Najem
on 21 Dec 2020
Walter Roberson
on 21 Dec 2020
GPU cannot help with symbolic calculations.
You have not really indicated what kind of calculations you are doing.
Walter Roberson
on 22 Dec 2020
I need to generate code of neural network feedforwad and also backword peocesses in symbolic form
... Why??
The formulas are going to be very complicated, and they are going to be pretty much indecipherable. You generate some big ratios of polynomials and square them, and similar operations, and the symbolic Engine might well decide that it needs to expand everything out -- an expression by itself raised to a power might be left alone, but as soon as you start doing arithmetic then it has to worry because the arithemetic might give a 0. Divisions it might decide that it needs to express in lowest terms. Symbolic operations are not just building simple matrix formulas: every operation might be expanded.
Walter Roberson
on 22 Dec 2020
By the time you get to dL4, you are dealing with vectors of expressions involving over 69000 variables (w2 by itself is over 62000 variables so any matrix multiply by it is going to be at least that many variables.)
Your expressions are going to be very long and complicated. The symbolic engine is not that fast, and there isn't much that can be done to speed it up when you use matrices that big.
I tried taking the derivative of the first entry of dL4 with respect to one of the variables (a11 it turned out.) It took over an hour just to compute it. Formatting it for output took another 7 hours before I gave up and killed the computation.
Walter Roberson
on 23 Dec 2020
Taking one derivative of one entry of dL4 took my system about 18 minutes. You have over 65000 derivatives to take at the bottom of your code, each on a value that is more complicated than dL4 is. You would expect a minimum of 20 minutes per derivative, times 65000 derivivates, gives about 130000 minutes -> over 900 days.
Each derivative looks like it is going to be over 2 gigabytes to write out -> 130 petabytes .
Suppose that I whipped up something to run on distributed AWS and (somehow!!) managed to create that 130+ petabyte file tomorrow: what would you do with it?
Ali Najem
on 23 Dec 2020
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!