# How to carry a matrix in algebric form under “set delayed”

I have a small query and a related problem regarding the working methodology of Mathematica under set delayed/Do functions. Here is a simplified version of my problem.

a[0] = IdentityMatrix[9]
a[m_] := f[m]*a[0]
b[m_] := Inverse[a[m]]
Do[If[Tr[b[m - 1]] < 1, f[m] = x*x /. x -> m,
f[m] = x*x*x /. x -> m]; Print[a[m]], {m, 1, 3}]


a[m] is a 9 X 9 matrix having elements being functions of f[m], whereas for m = 0, a[0] is known in numerical form. a[m] passes through multiple complex steps involving inverse etc. and finally lands in a "Do" loop. Under "Do' loop, the value of f[m] is determined for successive values of m by using the matrix a[m-1] obtained in last step.

Loop works fine for m=1 cycle as a[0] is known in numerical form. The problem arises for higher m values when a[m] gets more and more complex, as under set delayed condition a[m] is carried to the "Do" loop in algebraic form (please correct me if I am wrong !) and there it is converted to the numerical form by substituting f[m]. Since in between lie the terms involving inverse etc. (of a matrix with algebraic terms), the code runs forever with no output.

Could there be a way to carry the value of R[m] to the equation of a[m] and get it converted to numerical form before carried through multiple steps to reach for next m cycle under Do loop, instead of current scenario ? Could there be another solution to this problem !

Will be thankful for any suggestion !

• I'm not sure I understand your problem. Can you describe it in words/math formulas? Thanks! – AccidentalFourierTransform Jul 22 '19 at 13:53
• thanks for your interest ! Problem is very simple. a[m] is a 9X9 matrix with algebraic elements. Later terms require its inverse, which is very difficult to calculate if its elements are not numbers. Substituting R[m] can convert its elements to numerical values but it happens only in the last step under Do loop. I am looking for a method by which I can take the value of R[m] from Do loop and substitute it for a[m] before it goes through the inverse operation. – user49535 Jul 22 '19 at 15:26
• looks like f[m] is a scalar, yes? – kglr Jul 22 '19 at 20:15
• I'm a bit lost here, but this seems to be an iterated function system, where the result depends on the previous result. Nest and related functions handle this pattern better than Do. – John Doty Jul 22 '19 at 20:50

Looks like f[m] is a scalar.

If that's the case, Tr[f[m] mat] is f[m] Tr[mat] and Tr[Inverse[f[m] mat] is (1/f[m])Tr[Inverse[mat]]. So you need to do inversion only once to get Tr[Inverse[a[0]]]:

trai0 = Tr[Inverse @ a[0]];

Table[If[trai0 < f[m - 1], f[m] = m^2, f[m] = m^3]; a[m], {m, 1, 3}]


or

Do[If[trai0 < f[m - 1] , f[m] = m^2, f[m] = m^3]; Print[a[m]], {m, 1, 3}]


Note: For the example in OP, (a[0] = IdentityMatrix[9]) Tr[Inverse @ a[0]] is just 9.