Skip to content

Instantly share code, notes, and snippets.

@joslloand
Created September 13, 2016 18:58
Show Gist options
  • Save joslloand/3201681bad6e1b9d206ab6017839d017 to your computer and use it in GitHub Desktop.
Save joslloand/3201681bad6e1b9d206ab6017839d017 to your computer and use it in GitHub Desktop.
/*
compare a decoder matrix to the pseudoinverse of an encoder matrix
of the same dimensions.
*/
~cart = [
[1, 1, 1],
[-1, 1, 1],
[-1, -1, 1],
[1, -1, 1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, -1],
[1, -1, -1],
];
// dirs of a cube - in degrees
~dirs = ~cart.collect({ arg arr;
arr.asCartesian.asSpherical
}).collect({ arg sph;
[sph.theta, sph.phi].raddeg
});
~dirs.do(_.postln)
// newPeri
~d1 = FoaDecoderMatrix.newPeri(k: \velocity)
// newDiam - use this
~d2 = FoaDecoderMatrix.newDiametric(~dirs[0..3].degrad, \velocity)
~d1.dirChannels.raddeg.do(_.postln)
~d2.dirChannels.raddeg.do(_.postln)
~d1.matrix
~d2.matrix
// pseudo-inverse of newDiam
~e1 = FoaEncoderMatrix.newDirections(~d2.dirChannels)
~e1.dirChannels.raddeg.do(_.postln)
~e1.matrix;
~pinv = ~e1.matrix.pseudoInverse;
// compare to ~d2 matrix
~d2.matrix
// pseudo-inverse of newDirections
~e2 = FoaEncoderMatrix.newDirections(~dirs.degrad)
~e2.dirInputs.raddeg.do(_.postln)
~e2.matrix;
~pinv = ~e2.matrix.pseudoInverse;
// compare to ~d2 matrix
~d2.matrix
(// for every row, find a matching row in the other matrix
var results = [];
// var comparToD1 = ~d2.matrix;
var comparToD1 = ~pinv;
~d1rows = ~d1.matrix.rows.collect{|i| ~d1.matrix.getRow(i)};
comparToD1.rows.do{|i|
var thisRow, result;
thisRow = comparToD1.getRow(i);
result=false;
~d1rows.do{|testRow, j|
result.not.if{ // skip if match found
result = testRow.size.collect({ |k|
thisRow[k].equalWithPrecision(testRow[k], 1e-16)
}).includes(false).not;
}
};
results = results.add(result)
};
postf( "Matrices Match?? %\n", results.includes(false).not );
nil;
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment