2

Expand floods all my 64GB RAM in MMA 12.1 (Windows) just by sorting the powers of 16 variables. Somebody with >64GB RAM could run it. A memory saving alternative would even be more interesting.

a=(x[1]-x[11])^2+(x[2]-x[12])^2+(x[3]-x[13])^2+(x[4]-x[14])^2;
b=(x[1]-x[21])^2+(x[2]-x[22])^2+(x[3]-x[23])^2+(x[4]-x[24])^2;
c=(x[1]-x[31])^2+(x[2]-x[32])^2+(x[3]-x[33])^2+(x[4]-x[34])^2;
d=(x[21]-x[31])^2+(x[22]-x[32])^2+(x[23]-x[33])^2+(x[24]-x[34])^2;
e=(x[11]-x[31])^2+(x[12]-x[32])^2+(x[13]-x[33])^2+(x[14]-x[34])^2;
f=(x[11]-x[21])^2+(x[12]-x[22])^2+(x[13]-x[23])^2+(x[14]-x[24])^2;
g=1/12*Sqrt[4*a*b*c-a*(b+c-d)^2-b*(a+c-e)^2-c*(a+b-f)^2+(b+c-d)*(a+c-e)*(a+b-f)];
g2=g /. x[i_] -> (z[i] - μ[i]) t + μ[i];
taylor= (Series[g2, {t,0,2}] // Normal) /. t -> 1;

(the next line exceeds 64 GB RAM) taylor=Expand[taylor]; mean= taylor //. z[i_]^2 -> σ^2 + μ[i]^2; mean= mean //. z[i_] -> μ[i]; FullSimplify[mean==g+[Sigma]^2 (4(ab+ac+bc+ed)-(a+b-f)^2-(a+c-e)^2-(b+c-d)^2-(e+d-f)^2)/(288g) /. x[i_] -> [Mu][i]]

Memory overflow happens after about 15min on a modern CPU. The code is adapted from here and here.

If the code outputs True then your code fix is right or you had enough memory.

In case you want to test the code and you need a running example without high memory demands then set in the 7th line g=Sqrt[a];.

The result will be different with and without Expand, meaning that this command cannot be omitted.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
granular_bastard
  • 593
  • 4
  • 12
  • Could you approximate all the terms to order 2 in t and then substitute into g? – mikado Jan 12 '21 at 19:03
  • 3
    Try using Sqrt[Series[g^2, {t, 0, 2}]] instead of Series[g, {t, 0, 2}]. – Carl Woll Jan 12 '21 at 19:17
  • The Expand command is memory consuming. Moving the sqrt does not solve the problem. – granular_bastard Jan 12 '21 at 23:12
  • 2
    What is the purpose of this calculation? What will you do with the result especially since the simplified result will probably take up many GB of RAM? – Somos Jan 13 '21 at 02:52
  • 1
    I don't understand this part of your code: taylor=(Series[g,{t,0,2}]//Normal)/.t -> 1;. You first expand $g$ with respect to $t$ about the point $t=0$, but then you replace $t$ in the expansion with $1$? Would it not be more accurate to expand directly around $1$? – MarcoB Jan 13 '21 at 15:21
  • It is a multivariable Taylor Expansion, look here: https://mathematica.stackexchange.com/a/15035/69288 – granular_bastard Jan 13 '21 at 15:25
  • If you substitute z[i_]^2 -> σ^2 + μ[i]^2, how can you then later do z[i_] -> μ[i] ? That contradicts each other, right? – Rolf Mertig Jan 15 '21 at 22:09
  • z[i_]^2 -> σ^2 + μ[i]^2 replaces only powers of 2 – granular_bastard Jan 15 '21 at 22:16

2 Answers2

2

To avoid confusion I post another answer. The OP is right: the Expand is needed for the correct replacement of the square z terms. You can do the given task with 64 GB of RAM. One way is to do Expand not on the whole expression but in 'chunks', that is on the parts appearing within square roots, nominators and denominators separately. Begin with above code:

a = (x[1] - x[11])^2 + (x[2] - x[12])^2 + (x[3] - x[13])^2 + (x[4] - x[14])^2;
b = (x[1] - x[21])^2 + (x[2] - x[22])^2 + (x[3] - x[23])^2 + (x[4] - x[24])^2;
c = (x[1] - x[31])^2 + (x[2] - x[32])^2 + (x[3] - x[33])^2 + (x[4] - x[34])^2;
d = (x[21] - x[31])^2 + (x[22] - x[32])^2 + (x[23] - x[33])^2 + (x[24] - x[34])^2;
e = (x[11] - x[31])^2 + (x[12] - x[32])^2 + (x[13] - x[33])^2 + (x[14] - x[34])^2;
f = (x[11] - x[21])^2 + (x[12] - x[22])^2 + (x[13] - x[23])^2 + (x[14] - x[24])^2;
g = 1/12*Sqrt[4*a*b*c - a*(b + c - d)^2 - b*(a + c - e)^2 - 
 c*(a + b - f)^2 + (b + c - d)*(a + c - e)*(a + b - f)];
g = g /. x[i_] -> (z[i] - \[Mu][i]) t + \[Mu][i];
taylor = (Series[g, {t, 0, 2}] // Normal) /. t -> 1

the resulting expression appears as short output, -> Show All. Then identify nine terms, copy them 'by hand' into variables t1,...,t9. I worked from the back. Denominators, nominators and brackets within square roots. The result has the following structure:

(1/6)*Sqrt[t9] + (Sqrt[t8]*t7)/t6 + (1/12)*Sqrt[t5]*(-(t4/t3) + t2/t1).

Treat the variables all like

t1 = (((t1 // Expand) //. z[i_]^2 -> \[Sigma]^2 + \[Mu][i]^2) //.z[i_] -> \[Mu][i]);

The total result can be simplified as t9=t8=t5 to

Sqrt[t9]*(1/6 + t7/t6 + (1/12)*(t2/t1 - t4/t3))

and evaluate above expression. It will be VERY large, takes a long time to even display (the nb file will be about half a GB). I would not try Simplification on the whole expression, but maybe on the parts t1...t9 after the replacement of z^2 and z. So it can be done, but will it be useful?

Andreas
  • 3,297
  • 1
  • 5
  • 12
  • Could this process be automated? – granular_bastard Jan 16 '21 at 22:13
  • I don't know how.I tried ExpandNominator, but it used up the 64GB as well. To get it done in the described way takes maybe an hour or two and the running time depends on Simplify...leave it over night. – Andreas Jan 16 '21 at 22:19
  • I modified the code in the OP and now you can check if your solution is right (see there the changes g and g2 in lines 7-9). Actually the solution is not so large and was found by other methods but I want that MMA also solves it. – granular_bastard Jan 16 '21 at 22:48
  • @granularbastard At least I got numerical equality of the large expression and your g+sigma^2(...) by giving the mus and sigma random values between 0 and 1.Reducing all these expanded squares in the big expression seems an impossible task to me. – Andreas Jan 17 '21 at 12:10
1

Try this:

a = (x[1] - x[11])^2 + (x[2] - x[12])^2 + (x[3] - x[13])^2 + (x[4] - x[14])^2; 
b = (x[1] - x[21])^2 + (x[2] - x[22])^2 + (x[3] - x[23])^2 + (x[4] - x[24])^2; 
c = (x[1] - x[31])^2 + (x[2] - x[32])^2 + (x[3] - x[33])^2 + (x[4] - x[34])^2; 
d = (x[21] - x[31])^2 + (x[22] - x[32])^2 + (x[23] - x[33])^2 + (x[24] - x[34])^2; 
e = (x[11] - x[31])^2 + (x[12] - x[32])^2 + (x[13] - x[33])^2 + (x[14] - x[34])^2; 
f = (x[11] - x[21])^2 + (x[12] - x[22])^2 + (x[13] - x[23])^2 + (x[14] - x[24])^2; 
g = (1/12)*Sqrt[4*a*b*c - a*(b + c - d)^2 -  b*(a + c - e)^2 + (b + c - d)*
(a + c - e)*(a + b - f) - c*(a + b - f)^2]; 
g = g /. x[i_] -> (z[i] - \[Mu][i])*t + \[Mu][i]; 
taylor = Normal[Series[g, {t, 0, 2}]] /. t -> 1;
mean = taylor //. z[i_]^2 -> \[Sigma]^2 + \[Mu][i]^2; 
mean = Simplify[mean //. z[i_] -> \[Mu][i]]

just omit the expansion

Andreas
  • 3,297
  • 1
  • 5
  • 12
  • this produces different results, try in line 7 with a simpler function g=Sqrt[a]; and you get with and without Expand different solutions – granular_bastard Jan 16 '21 at 17:02