Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linAlg::dot() method has return type too narrowly declared #5

Closed
sneakyimp opened this issue Dec 27, 2022 · 0 comments
Closed

linAlg::dot() method has return type too narrowly declared #5

sneakyimp opened this issue Dec 27, 2022 · 0 comments

Comments

@sneakyimp
Copy link

The function linAlg::dot needs to have its return type changed to include float and possible int and/or other scalar types. It is possible to multiply two vectors and get a scalar result. E.g., this code in matlab/octave returns a scalar, and blas returns a float:

octave:25> m1 = [1 2 3]
m1 =

   1   2   3

octave:26> m2 = [1;2;3]
m2 =

   1
   2
   3

octave:27> m1 * m2
ans = 14

This code should return 14:

$m1 = Np\vector::ar([1, 2, 3]);
echo "$m1\n";
$m2 = Np\vector::ar([1, 2, 3]);
echo "$m2\n";

$v = $m1->dot($m2);
echo "$v\n";

but due to the narrow return type restriction in linAlgb::dot, it throws this error:

PHP Fatal error:  Uncaught TypeError: Np\vector::dot(): Return value must be of type Np\matrix|Np\vector, float returned in /Users/sneakyimp/Desktop/biz/machine-learning/np/vendor/ghostjat/np/src/linAlgb/linAlg.php:34

Simply modifying the linAlg::dot function as follows should fix this particular error:

    /**
     *
     * get dot product of m.m | m.v | v.v
     *
     * @param \Np\matrix|\Np\vector $d
     * @return matrix|vector|float
     */
    public function dot(matrix|vector $d): matrix|vector|float {
        if ($this instanceof matrix) {
            if ($d instanceof matrix) {
                return $this->dotMatrix($d);
            }
            return $this->dotVector($d);
        }
        return blas::dot($this, $d);
    }
@sneakyimp sneakyimp changed the title linAlb::dot() method has return type too narrowly declared linAlg::dot() method has return type too narrowly declared Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants