Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

- 4.0.0
- Made the pure-PHP library byte-compatible with the tensor_ext extension for `serialize()` / `unserialize()`: `Vector`, `Matrix`, and `ColumnVector` now implement `__serialize()` and `__unserialize()` so that payloads written by either implementation can be read by the other.
- Renamed the `build()` / `quick()` factories to `Vector::fromArray(array $a = [], bool $validate = true)` and `Matrix::fromArray(array $a = [], bool $validate = true)` to match the Tensor-Ext API.

- 3.1.0
- Implemented the singular value decomposition (SVD) in the pure-PHP library
- Implemented the Moore-Penrose pseudoinverse in the pure-PHP library via SVD
Expand Down
31 changes: 26 additions & 5 deletions docs/Matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ Instantiate a matrix directly.
- **Throws:** `Tensor\Exceptions\InvalidArgumentException` if rows have unequal column counts
- **Note:** Prefer the factory methods below.

### `Matrix::build(array $a = []) : Matrix`
### `Matrix::fromArray(array $a = [], bool $validate = true) : Matrix`

Factory method to build a new matrix from an array, running validation.
Build a matrix from a PHP array of rows, normalising each value to a `float`
and (by default) validating rectangularity and casting non-floats. Pass
`$validate = false` to skip validation for quicker construction, e.g. when
the source is already known to be a well-formed `list<list<float>>`.

### `Matrix::quick(array $a = []) : Matrix`

Build a new matrix foregoing any validation for quicker instantiation.
- **Parameters:**
- `$a` — `array<array<int|float>>`
- `$validate` — whether to validate rows and cast non-floats to `float` (default `true`)
- **Throws:** `Tensor\Exceptions\InvalidArgumentException` if `$validate = true` and rows have unequal column counts

### `Matrix::identity(int $n) : Matrix`

Expand Down Expand Up @@ -135,6 +139,23 @@ Return the elements of the matrix in a 2-d array.

- **Returns:** `list<list<float>>`

### `asTensorBuffer() : TensorBuffer`

Return the underlying elements (flattened in row-major order) wrapped as a
`Tensor` `TensorBuffer`, mirroring the Tensor-Ext surface area.

- **Returns:** `Tensor\TensorBuffer`

### `__serialize() : array`

Return the elements of the matrix as a plain PHP array of rows so that only
the values, and not the object structure, appear in the serialized form.
Output is byte-compatible with the `Tensor-Ext` polyfill.

### `__unserialize(array $data) : void`

Restore the matrix from a plain array of rows produced by `__serialize()`.

### `asVectors() : array`

Return each row as a vector in an array.
Expand Down
35 changes: 25 additions & 10 deletions docs/Vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ Instantiate a vector directly.
- `$validate` — whether to validate and cast elements to floats (default `true`)
- **Note:** Prefer the factory methods below.

### `Vector::build(array $a = [])`
### `Vector::fromArray(array $a = [], bool $validate = true)`

Factory method to build a new vector from an array, running validation.
Build a vector from a PHP array of elements, normalising each value to a
`float` and (by default) validating the input. Pass `$validate = false` to
skip validation for quicker construction, e.g. when the source is already
trusted and known to be a `list<float>`.

- **Parameters:** `$a` — `(int|float)[]`
- **Returns:** `mixed` (a `Vector`/`static`)

### `Vector::quick(array $a = [])`

Build a vector foregoing any validation for quicker instantiation.

- **Parameters:** `$a` — `(int|float)[]`
- **Parameters:**
- `$a` — `(int|float)[]`
- `$validate` — whether to cast non-floats to `float` (default `true`)
- **Returns:** `mixed` (a `Vector`/`static`)

### `Vector::zeros(int $n) : Vector`
Expand Down Expand Up @@ -140,6 +138,23 @@ Return the vector as an array.

- **Returns:** `list<float>`

### `asTensorBuffer() : TensorBuffer`

Return the underlying elements wrapped as a `Tensor` `TensorBuffer`, mirroring
the Tensor-Ext surface area.

- **Returns:** `Tensor\TensorBuffer`

### `__serialize() : array`

Return the elements of the vector as a plain PHP array so that only the values,
and not the object structure, appear in the serialized form. Output is
byte-compatible with the `Tensor-Ext` polyfill.

### `__unserialize(array $data) : void`

Restore the vector from a plain array of elements produced by `__serialize()`.

### `asRowMatrix() : Matrix`

Return this vector as a 1 x n row matrix.
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ use Tensor\Matrix;
use Tensor\Vector;

// Build a 2 x 3 matrix.
$a = Matrix::build([
$a = Matrix::fromArray([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
]);

// Build a 3 x 2 matrix.
$b = Matrix::build([
$b = Matrix::fromArray([
[7.0, 8.0],
[9.0, 10.0],
[11.0, 12.0],
Expand Down
26 changes: 13 additions & 13 deletions src/ColumnVector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function n() : int
*/
public function transpose()
{
return Vector::quick($this->a);
return Vector::fromArray($this->a, false);
}

/**
Expand Down Expand Up @@ -84,7 +84,7 @@ public function multiplyMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -115,7 +115,7 @@ public function divideMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -146,7 +146,7 @@ public function addMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -177,7 +177,7 @@ public function subtractMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -208,7 +208,7 @@ public function powMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -239,7 +239,7 @@ public function modMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -271,7 +271,7 @@ public function equalMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -303,7 +303,7 @@ public function notEqualMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -335,7 +335,7 @@ public function greaterMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -367,7 +367,7 @@ public function greaterEqualMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -399,7 +399,7 @@ public function lessMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}

/**
Expand Down Expand Up @@ -431,6 +431,6 @@ public function lessEqualMatrix(Matrix $b) : Matrix
$c[] = $rowC;
}

return Matrix::quick($c);
return Matrix::fromArray($c, false);
}
}
2 changes: 1 addition & 1 deletion src/Decompositions/Cholesky.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function decompose(Matrix $a) : self
}
}

$l = Matrix::quick($l);
$l = Matrix::fromArray($l, false);

return new self($l);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Decompositions/Eigen.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function decompose(Matrix $a, bool $symmetric = false) : self
$n = $a->n();

if ($n === 1) {
return new self($a->rowAsVector(0)->asArray(), Matrix::quick([[1.0]]));
return new self($a->rowAsVector(0)->asArray(), Matrix::fromArray([[1.0]], false));
}

if ($symmetric) {
Expand Down Expand Up @@ -83,7 +83,7 @@ public static function decompose(Matrix $a, bool $symmetric = false) : self
}
}

return new self($d, Matrix::quick($rows));
return new self($d, Matrix::fromArray($rows, false));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Decompositions/LU.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public static function decompose(Matrix $a) : self
}

return new self(
Matrix::quick($l),
Matrix::quick($u),
Matrix::quick($p)
Matrix::fromArray($l, false),
Matrix::fromArray($u, false),
Matrix::fromArray($p, false)
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Decompositions/SVD.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ public static function decompose(Matrix $a) : self
}

return new self(
Matrix::quick($uMatrix),
Matrix::fromArray($uMatrix, false),
array_slice($singularValues, 0, $k),
Matrix::quick($vT)
Matrix::fromArray($vT, false)
);
}

Expand Down Expand Up @@ -307,7 +307,7 @@ public function s() : Matrix
$s[$i][$i] = $value;
}

return Matrix::quick($s);
return Matrix::fromArray($s, false);
}

/**
Expand Down
Loading
Loading