In my code, I programmatically construct a Dimensions $(3,m)$ matrix, called mtxA. I subsequently take its Transpose, forming an $(m,3)$ matrix, so that it can be dotted into a vector with $3$ elements.

But sometimes, $m$ is zero, which means mtxA looks like this :

mtxA = {{},{},{}};

(* {3, 0} *)

But now, if I take its Transpose, I get a wrong result:

% // Dimensions

(* {} *)
(* {0} *)

Instead of a $(0,3)$ matrix, I have a $(0)$ vector. And so when I try to Dot this into a 3-vector, I get a cascade of Dot::dotsh errors because you obviously can't dot {} into a 3-vector.

  1. Is this because you can't represent a $(0,3)$ matrix in Mathematica?
  2. Is it possible to tell Transpose to preserve the rank of my matrix?
  3. How do I deal with this problem in my code without writing a bunch of If statements to treat the $m=0$ case separately?
  • $\begingroup$ What would a $(0, 3)$ matrix mean in this case? It'd have no elements, so the $3$ wouldn't make sense. $\endgroup$
    – b3m2a1
    Nov 18, 2017 at 20:13
  • $\begingroup$ What would $m_{0 \times 3} \cdot \mathbb{1}_{3 \times 1}$ return? $\endgroup$
    – Edmund
    Nov 19, 2017 at 16:44
  • $\begingroup$ @Edmund It would return a $b_{0\times 1}$... duh! ;p $\endgroup$
    – QuantumDot
    Nov 19, 2017 at 16:56
  • $\begingroup$ Perhaps you could cook something up by using Null for your zero dimension. For example m = {{Null}, {Null}, {Null}} and then Transpose@m . {{1}, {2}, {3}} gives {{6 Null}} which you could, with ReplaceAll, get {{Null}} which you could in some way interpret as having dimension $0 \times 1$. Seems a bit messy though. Overall I think @user42582 has it correct in saying that it is not directly possible; until shown otherwise. $\endgroup$
    – Edmund
    Nov 19, 2017 at 17:12

1 Answer 1

  1. Dimensions returns the appropriate dimensions for full arrays (see Dimensions). Executing Depth[{}] returns 2 which is interpreted as 'it takes 1 index to specify every possible level in expression {} (see Depth). Also, FullForm[{}] returns (not surprisingly) List[]. Having said all that, it is my guess that it is not possible to define an expression in Mathematica with dimensions {0,3} because that would have to be interpreted as describing an expression that on level 1 has no parts while on level 2 there are 3 available parts, which sounds contradictory (to me).
  2. I think Transpose (much like Flatten) cannot preserve the dimensions of arrays without full dimensions on all levels and an array like {{},{},{}} is one such case.
  3. Unless there are more details, a single If checking the dimesions of mtxA might be sufficient; otherwise you could define two different functions eg proc[mat_?(MatchQ[{{}, {}, {}}, #] &)] and proc[other_] and let Mathematica decide which one it should call, depending on the input. The drawback of this approach is that you'd have to essentially duplicate much of the code that depends on mtxA.
  • 1
    $\begingroup$ There is no need to delete. Direct answer may be more suited for OP as duplicates don't need to match word by word. $\endgroup$
    – Kuba
    Nov 28, 2017 at 18:09

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