- Edited
I appreciate you taking the time for the detailed explanation. It has been a very long time since I took a class that bothered to discuss tensors or matrix operations in any detail, but I get the impression that this mathematical concept involves a level of desirable abstraction that goes a step beyond my prior exposure to matrix operations. What I need at the moment is for my PHP code to replicate the behavior I see in my octave/matlab code. It seems to me the distinction between vectors and matrices doesn't really exist in octave/matlab. I.e., octave treats [1,1,1,1] as a 1x4 matrix:
x1 = [1,1,1,1]
size(x1)
ans =
1 4
I have always had great difficulty performing these operations in my head and juggling transpositions and arithmetic operations and their resulting dimensions is nearly impossible for me -- I wonder if perhaps it might reveal some undiagnosed dyslexia on my part.
Weedpacket So ... try multiplying [[1,1,1,1]] by [[1],[3],[5],[7]] and vice versa and compare the results with the matlab ones
This PHP code:
$m1 = new NumArray([[1,1,1,1]]);
echo "$m1\n";
$m2 =new NumArray([[1],[3],[5],[7]]);
echo "$m2\n";
$v = $m1->dot($m2);
echo "$v\n";
Yields this output:
NumArray([
[1, 1, 1, 1]
])
NumArray([
[1],
[3],
[5],
[7]
])
NumArray([
[16]
])
Octave barfs on the results, as it should, because these matrices are both 1x4:
x1 = [[1,1,1,1]]
x2 = [[1],[3],[5],[7]]
x1 * x2
error: operator *: nonconformant arguments (op1 is 1x4, op2 is 1x4)
This barfing is a helpful guide rail for me, and helps me to understand that I need a transposition, or have my dimensions reversed. On the other hand, when such an operation succeeds, this tends to suggest that I am multiplying the right numbers together.
Octave/matlab does in fact use the asterisk for matrix multiplication and has a dot function to perform a dot product. It is noteworthy that the documentation mentions vectors here:
dot (x, y, dim)
Compute the dot product of two vectors.
If x and y are matrices, calculate the dot products along the first non-singleton dimension.
If the optional argument dim is given, calculate the dot products along this dimension.
Implementation Note: This is equivalent to sum (conj (X) .* Y, dim), but avoids forming a temporary array and is faster. When X and Y are column vectors, the result is equivalent to X' * Y. Although, dot is defined for integer arrays, the output may differ from the expected result due to the limited range of integer objects.
Weedpacket Incidentally, in your tests just a little further up, you echoed $m1 instead of $m2 in the last test. I would be interested to know what PHPNum reckons the transpose of a vector looks like.
Sorry for the mistake. I can confirm this weirdness. This PHP code:
$m1 = new NumArray([1,3,5,7]);
echo "$m1\n";
$m2 = $m1->getTranspose();
echo "$m2\n";
Yields this result:
NumArray([1, 3, 5, 7])
NumArray([1, 3, 5, 7])
It appears that the two matrices are internally identical. This PHP code:
$m1 = new NumArray([1,3,5,7]);
print_r($m1);
$m2 = $m1->getTranspose();
print_r($m2);
Shows two identical objects:
NumPHP\Core\NumArray Object
(
[shape:protected] => Array
(
[0] => 4
)
[data:protected] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 7
)
[cache:protected] => Array
(
)
)
NumPHP\Core\NumArray Object
(
[shape:protected] => Array
(
[0] => 4
)
[data:protected] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 7
)
[cache:protected] => Array
(
)
)