4

I'm trying to speed up my Code, but I'm facing problems with changing the AppendTo section in my code with something "Faster". I've read through some other examples that explain the use of Reapand Sow. Understanding Sow and Reap documentation
Writing Faster Mathematica Code - Sow and Reap?
https://blog.wolfram.com/2011/12/07/10-tips-for-writing-fast-mathematica-code/

I can't figure out how to apply the Solutions given in the links for my problem.

n = 20;
dz = 1000/n;
RollLM[l_, EM_, Ixx_, p_] := {{1, l, Power[l, 2] / (2 EM Ixx), 
Power[l, 3] / (6 EM Ixx), p Power[l, 4] / (24 EM Ixx)}, {0, 1, l / (EM 
Ixx), Power[l, 2] / (2 EM Ixx), p Power[l, 3] / (6 EM Ixx)}, {0, 0, 1, 
l , p Power[l, 2] / 2}, {0, 0, 0, 1, p l}, {0, 0, 0, 0, 1}};
BearLM[d_, c_] := {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, d, 1, 0, 0}, 
{-c, 0, 0, 1, 0}, {0, 0, 0, 0, 1}};
pBF = Function[z, Piecewise[{{989000/500, 250 <= z <= 750}}]];

Vec1A = {vA, PhiA, 0, 0, 1};
Vec2A = BearLM[810000, 850000].Vec1A;
VecAList = {Vec2A};
Do[ AppendTo[VecAList, RollLM[dz, 210000, 262440000 Pi, pBF[(i- 
0.5)dz]] .VecAList[[i]]], {i, n}];
Vec3A = BearLM[810000, 850000].Last[VecAList];
SolA = Solve[{Vec3A[[3]] == 0, Vec3A[[4]] == 0},{vA,PhiA}] 
[[1]];
CR36
  • 127
  • 9
  • Are a, b, vA, RF, B,PhiA meant to be numerical values? Then please define them first. That will speed up already quite a lot. – Henrik Schumacher Jan 10 '20 at 12:12
  • I changed a, b, RF and B to the numerical values. I justed overlocked to change them in the first place. But PhiA and vA will be later one solved in the Code. – CR36 Jan 10 '20 at 12:18
  • ListLinePlot[LeafCount /@ VecAList] tells me that this is not a good idea. Better numericise PhiA and vA and use a numerical linear solver instead. – Henrik Schumacher Jan 10 '20 at 12:21
  • I've added the example so it is better understandable. – CR36 Jan 10 '20 at 12:33

2 Answers2

6

You can replace the AppendTo loop with

u = Vec2A;
VecAList = Table[
   u = RollLM[dz, 210000, 262440000 Pi, pBF[(i - 0.5) dz]].u,
   {i, 1, n}];

However, this does not speed up anything as the computations are performed symbolically and this LeafCount[VecAList[[i]] grows exponentially with i (and thus LeafCount[VecAList] grows exponentially with n).

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
6

It doesn't look like AppendTo has much overhead in this case.

n = 20;
dz = 1000/n;
RollLM[l_, EM_, Ixx_, p_] := {{1, l, Power[l, 2] / (2 EM Ixx), Power[l, 3] / (6 EM Ixx), p Power[l, 4] / (24 EM Ixx)}, {0, 1, l / (EM Ixx), Power[l, 2] / (2 EM Ixx), p Power[l, 3] / (6 EM Ixx)}, {0, 0, 1, l , p Power[l, 2] / 2}, {0, 0, 0, 1, p l}, {0, 0, 0, 0, 1}};
BearLM[d_, c_] := {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, d, 1, 0, 0}, {-c, 0, 0, 1, 0}, {0, 0, 0, 0, 1}};
pBF = Function[z, Piecewise[{{989000/500, 250 <= z <= 750}}]];

Vec1A = {vA, PhiA, 0, 0, 1};
Vec2A = BearLM[810000, 850000].Vec1A;


VecAList = {Vec2A};
Timing[Do[AppendTo[VecAList, RollLM[dz, 210000, 262440000 Pi, 
     pBF[(i - 0.5) dz]].VecAList[[i]]], {i, n}]]

{0.03125, Null}

original = VecAList;

VecAList = {Vec2A};
Timing[Do[VecAList = {VecAList, RollLM[dz, 210000, 262440000 Pi,
     pBF[(i - 0.5) dz]].Last[VecAList]}, {i, n}]]

{0.03125, Null}

VecAList = Partition[Flatten[VecAList], 5];
original == VecAList

True

Chris Degnen
  • 30,927
  • 2
  • 54
  • 108