1
$\begingroup$

Abstract I have a function eqsfromMatrix[...] that does exactly as it sounds. It returns a list of equations given matrix m for different augmented vectors in b so that eqs = m.vars == b. It takes last few letters in alphabet to form the variables i.e. {x, y, z}. If b not supplied but set to b = 1 in arguments it takes the first few letters in alphabet to form scalers b = {a,b,c} or if b set in arguments as b = 0 it will supply list of zeroes b = {0, 0, 0,...}. It starts using letters for variables where I set a pivot which is a desired soft start point "x".

Problem is it won't work if the length of the matrix m is longer than the position of the pivot in the supplied alphabet.

About the Code If using m1 and b1 and I replace vars = Take[abc, {start[m], end[m]}]; with vars = Take[abc, {23, 26}]; it works just fine.

What am I doing wrong?

eqsfromMatrix[mat_, b_ : None, piv_ : "x"] := 
 Module[{len, abc, pivot, diff, vars, scals, eqs},
  len = Length[mat];
  abc = Alphabet[];
  pivot = Flatten[Position[abc, piv]][[1]]; (* desired soft start point at x returns 24 *)
  start[m_] := pivot /; len <= 3;
  start[m_] := Length[abc] - len + 1;
  end[m] = start[m] + len - 1;
  vars = Part[abc, {start[m], end[m]}];
  eqs = mat.vars;
  scals = 
   Switch[b, None, Return[eqs], _List, b, 1, Take[abc, {1, len}], 0, 
    Table[0, len]];
  Table[eqs[[i]] == scals[[i]], {i, 1, len}]
  ]

ClearAll[m1,m2,b1,b2]
m1 = {{1, 0, 1, 3}, {-1, 3, 2, 1}, {3, 2, 4, 5}, {8, 3, 5, 3}};
b1 = {-1, 3, 2, 4};(* m1, b1 work , length is longer than pivot position from end of alphabet *)
m2={{1,2},{3,2}} 
b2={1,-2}(* m2, b2 work, length is shorter than pivot position from end of 
eqsfromMatrix[m1] (* dont work *)
eqsfromMatrix[m1,b1] (* dont work *)
eqsfromMatrix[m1,1] (* dont work *)
eqsfromMatrix[m1,0] (* dont work *)
eqsfromMatrix[m2] (* works *)
eqsfromMatrix[m2,b2] (* works *)
eqsfromMatrix[m2,1] (* works *)
eqsfromMatrix[m2,0] (* works *)
$\endgroup$
2
  • 1
    $\begingroup$ I think you might want to consider using indexed variables (say, x[1],x[2],... and \[Alpha][1],\[Alpha][2]...). If this is acceptable, you can use vars = Array[x, Last@Dimensions@m] and replace Take[abc, {1, len}] with Array[\[Alpha], len] when defining scals. $\endgroup$ – kglr Jun 15 '20 at 2:39
  • $\begingroup$ wonderful idea. thank you. $\endgroup$ – Jules Manson Jun 15 '20 at 3:21
2
$\begingroup$

Your code block with the fixes:

ClearAll[eqsfromMatrix]
eqsfromMatrix[mat_, b_ : None, piv_ : "x"] := Module[{len = Length[mat],
    abc = Alphabet[], pivot, vars, scals, eqs, start, end},
  pivot = Flatten[Position[abc, piv]][[1]];
  start = If[len <= 3, pivot, Length[abc] - len + 1];
  end = start + len - 1;
  vars = Part[abc, start ;; end]; (* or Take[abc, {atart, end}]; *)
  eqs = mat.vars;
  scals = Switch[b, None, Return[eqs], _List, b, 
    1, Take[abc, {1, len}], 0, Table[0, len]];
  Table[eqs[[i]] == scals[[i]], {i, 1, len}]]

eqsfromMatrix[m1]
 {"w" + "y" + 3 "z", -"w" + 3 "x" + 2 "y" + "z", 
  3 "w" + 2 "x" + 4 "y" + 5 "z", 8 "w" + 3 "x" + 5 "y" + 3 "z"}
eqsfromMatrix[m1, b1] 
 {"w" + "y" + 3 "z" == -1,
-"w" + 3 "x" + 2 "y" + "z" == 3, 
 3 "w" + 2 "x" + 4 "y" + 5 "z" == 2, 
 8 "w" + 3 "x" + 5 "y" + 3 "z" == 4}
eqsfromMatrix[m1, 1] 
 {"w" + "y" + 3 "z" == "a",
  -"w" + 3 "x" + 2 "y" + "z" == "b", 
  3 "w" + 2 "x" + 4 "y" + 5 "z" == "c", 
  8 "w" + 3 "x" + 5 "y" + 3 "z" == "d"}
eqsfromMatrix[m1, 0] 
 {"w" + "y" + 3 "z" == 0, 
 -"w" + 3 "x" + 2 "y" + "z" == 0, 
 3 "w" + 2 "x" + 4 "y" + 5 "z" == 0, 
 8 "w" + 3 "x" + 5 "y" + 3 "z" == 0}
$\endgroup$
2
  • $\begingroup$ thank you so much. this is perfect. $\endgroup$ – Jules Manson Jun 15 '20 at 8:45
  • $\begingroup$ @JulesManson, my pleasure. Thank you for the accept. $\endgroup$ – kglr Jun 15 '20 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.