# Switching from AppendTo to Speed up

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[] == 0, Vec3A[] == 0},{vA,PhiA}]
[];

• 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 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 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 at 12:21
• I've added the example so it is better understandable. – CR36 Jan 10 at 12:33

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

• So you are suggesting that the actuall solving is the factor that costs a lot of time? – CR36 Jan 10 at 12:46
• I guess so. The second version does not use AppendTo and the timing is the same. – Chris Degnen Jan 10 at 12:48
• So is there a better way to solve this system with Symbolic variables and also the n variable witch in this case is 20 can be 250 and then it's getting pretty slow. – CR36 Jan 10 at 12:57
• That's kind of a different question. – Chris Degnen Jan 10 at 13:03
• @CR36 The solving takes a significant amount of time, but the dominant factor seems to be RollLM[..].VecAList[[i]], as Henrik hinted at. – Michael E2 Jan 10 at 14:22

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).

• So is there something you can recomend? – CR36 Jan 10 at 16:07
• That's kind of a different question. – Henrik Schumacher Jan 10 at 16:32
• "[K]ind of" in the sense of "very much". Would be best to scrap this question and start afresh. – Daniel Lichtblau Jan 10 at 17:07
• I just meant to cite @ChrisDegnen... ;) – Henrik Schumacher Jan 10 at 17:21