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
33 changes: 33 additions & 0 deletions benchmarks/Factories/VectorLinspaceBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tensor\Benchmarks\Factories;

use Tensor\Vector;
use Generator;

/**
* @Groups({"Factories"})
*/
class VectorLinspaceBench
{
/**
* @return list<array{size: int}>
*/
public function sizes() : Generator
{
yield 'small' => ['size' => 1024 * 1024];
yield 'medium' => ['size' => 4096 * 4096];
}

/**
* @Subject
* @Iterations(5)
* @ParamProviders({"sizes"})
* @OutputTimeUnit("milliseconds", precision=3)
* @param array $params
*/
public function linspace(array $params) : void
{
Vector::linspace(0.0, 1000.0, $params['size']);
}
}
33 changes: 33 additions & 0 deletions benchmarks/Factories/VectorRangeBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tensor\Benchmarks\Factories;

use Tensor\Vector;
use Generator;

/**
* @Groups({"Factories"})
*/
class VectorRangeBench
{
/**
* @return list<array{size: int}>
*/
public function sizes() : Generator
{
yield 'small' => ['size' => 1024 * 1024];
yield 'medium' => ['size' => 4096 * 4096];
}

/**
* @Subject
* @Iterations(5)
* @ParamProviders({"sizes"})
* @OutputTimeUnit("milliseconds", precision=3)
* @param array $params
*/
public function range(array $params) : void
{
Vector::range(0.0, (float) $params['size'], 1.0);
}
}
93 changes: 93 additions & 0 deletions ext/include/factories.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,96 @@ void tensor_random_gaussian(zval * return_value, zval * n)

zval_ptr_dtor(&c);
}

/**
* Create a tensor with `n` evenly spaced values between `min` and `max`,
* inclusive of both endpoints. Values are accumulated from `min` toward
* `max`, with the final element forced to `max` to guarantee exact endpoint.
* The caller is responsible for rejecting n < 2 or min > max.
*/
void tensor_linspace(zval * return_value, zval * min, zval * max, zval * n)
{
zend_long length = zephir_get_intval(n);
double lo = zephir_get_doubleval(min);
double hi = zephir_get_doubleval(max);
zval c;

if (UNEXPECTED(tensor_tensorbuffer_create(return_value, length, &c) == FAILURE)) {
return;
}

double * vc = zephir_buffer_doubles(&c);

double step = fabs(hi - lo) / (double) (length - 1);

zend_long i;

vc[0] = lo;

for (i = 1; i < length - 1; ++i) {
vc[i] = vc[i - 1] + step;
}

vc[length - 1] = hi;

zval_ptr_dtor(&c);
}

/**
* Create a tensor mirroring PHP's `range($start, $end, $step)` semantics:
* values are `start + i*step` (or `start - i*|step|` for decreasing range),
* generated until the value crosses `end`. The caller must reject a zero step,
* a step with the wrong sign for the direction of traversal, and a step
* whose magnitude exceeds the span. Equal start/end yields a single-element
* tensor holding that value.
*/
void tensor_range(zval * return_value, zval * start, zval * end, zval * interval)
{
double s = zephir_get_doubleval(start);
double e = zephir_get_doubleval(end);
double st = fabs(zephir_get_doubleval(interval));

zval c;

if (s == e) {
if (UNEXPECTED(tensor_tensorbuffer_create(return_value, 1, &c) == FAILURE)) {
return;
}

double * vc = zephir_buffer_doubles(&c);

vc[0] = s;

zval_ptr_dtor(&c);

return;
}

int increasing = e > s;
double sign = increasing ? 1.0 : -1.0;

zend_long count = 0;
zend_long i;

for (i = 0; ; ++i) {
double el = s + ((double) i) * st * sign;

if ((increasing && el > e) || (!increasing && el < e)) {
break;
}

++count;
}

if (UNEXPECTED(tensor_tensorbuffer_create(return_value, count, &c) == FAILURE)) {
return;
}

double * vc = zephir_buffer_doubles(&c);

for (i = 0; i < count; ++i) {
vc[i] = s + ((double) i) * st * sign;
Comment thread
andrewdalpino marked this conversation as resolved.
}

zval_ptr_dtor(&c);
}
18 changes: 18 additions & 0 deletions ext/include/factories.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,22 @@ void tensor_random_uniform_pm1(zval * return_value, zval * n);
*/
void tensor_random_gaussian(zval * return_value, zval * n);

/**
* Allocate a fresh `Tensor\TensorBuffer` of `n` elements holding the evenly
* spaced values between `min` and `max` (inclusive of both endpoints). Values
* are accumulated from `min` toward `max`, with the final element forced to
* `max`. The caller must reject `n < 2` and `min > max`.
*/
void tensor_linspace(zval * return_value, zval * min, zval * max, zval * n);

/**
* Allocate a fresh `Tensor\TensorBuffer` mirroring PHP's
* `range($start, $end, $interval)`: values are `start ± i*|interval|`,
* generated until the value crosses `end`. Equal start/end yields a
* single-element buffer. The caller must reject a zero interval, an interval
* whose sign disagrees with the traversal direction (for increasing ranges),
* and an interval whose magnitude exceeds the span.
*/
void tensor_range(zval * return_value, zval * start, zval * end, zval * interval);

#endif
2 changes: 1 addition & 1 deletion ext/tensor/decompositions/cholesky.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/tensor/decompositions/eigen.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/tensor/decompositions/lu.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/tensor/decompositions/svd.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading