3
$\begingroup$

Is there a more elegant way of dealing with the following subscripting?

Do[threetest = 
MatrixRank[{{threepoints[[i]][[2]][[1]] - 
threepoints[[i]][[1]][[1]], 
threepoints[[i]][[2]][[2]] - threepoints[[i]][[1]][[2]]},
{threepoints[[i]][[3]][[1]] - threepoints[[i]][[1]][[1]],
threepoints[[i]][[3]][[2]] - threepoints[[i]][[1]][[2]]}}]; 
If[threetest < 2, Break[], 1], {i, Binomial[n, 3]}]
$\endgroup$
  • $\begingroup$ If the last dimension of threePoints is two, then you might try something like MatrixRank[threepoints[[i, 2 ;; 3]] - threepoints[[i, 1]]]. $\endgroup$ – WReach Apr 26 '19 at 18:29
11
$\begingroup$

If threepoints is defined with any kind of regularity, then threepoints[[i]][[2]][[1]] is the same as threepoints[[i,2,1]]. See the documentation of Part.

Further, you can do vector processing with ;;, along the lines of

Do[threetest = 
  MatrixRank[{threepoints[[i, 2, 1 ;; 2]] - threepoints[[i, 1, 1 ;; 2]], 
              threepoints[[i, 3, 1 ;; 2]] - threepoints[[i, 1, 1 ;; 2]]}]; 
  If[threetest < 2, Break[], 1],
  {i, Binomial[n, 3]}]

If the third coordinate of threetest has only two slots (that is, if Dimensions[threetest] returns {something,something,2}), then the above can also be written as

Do[threetest = 
  MatrixRank[{threepoints[[i, 2]] - threepoints[[i, 1]], 
              threepoints[[i, 3]] - threepoints[[i, 1]]}]; 
  If[threetest < 2, Break[], 1],
  {i, Binomial[n, 3]}]
$\endgroup$
6
$\begingroup$

The other thing I do is to use the stylized brackets in all of my MMA code. Stackexchange mangles it, so have to use a picture to demonstrate:

enter image description here

Big difference in readability.

$\endgroup$
  • $\begingroup$ Also, you can put them in the subscript and they work equally. And if it's still not enough, you can use the invisible comma to separate indices. ;-) $\endgroup$ – Veky Apr 27 '19 at 3:53
  • $\begingroup$ It’s probably worth adding how to enter these doubled brackets using the keyboard: Esc–[–[–Esc. $\endgroup$ – Stephen Powell Apr 27 '19 at 9:17

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