public function testMoreReturnsEmptyVectorWhenNoItems() { $vector = new Vector(); $more = $vector->more(); $this->assertInstanceOf('\\clojure\\lang\\Vector', $more); $this->assertEquals(0, $more->count()); }
public static function join() { $args = func_get_args(); if (empty($args)) { return ''; } $paths = new Vector(); foreach ($args as $arg) { if (is_string($arg)) { $arg = explode(self::delimiter, $arg); } if (is_an_array($arg)) { foreach ($arg as $fragment) { $paths->push($fragment); } continue; } $paths->push($arg); } $paths = $paths->select(function ($path) { $str = strval($path); return !empty($str); }); $joined_path = $paths->join(self::delimiter); if (S($args[0])->starts_with(self::delimiter)) { $joined_path->prependF(self::delimiter); } return $joined_path; }
private function _create_translation(Vector $vec) { $vtcX = new Vector(['dest' => new Vertex(array('x' => 1.0, 'y' => 0.0, 'z' => 0.0, 'w' => 0.0))]); $vtcY = new Vector(['dest' => new Vertex(array('x' => 0.0, 'y' => 1.0, 'z' => 0.0, 'w' => 0.0))]); $vtcZ = new Vector(['dest' => new Vertex(array('x' => 0.0, 'y' => 0.0, 'z' => 1.0, 'w' => 0.0))]); $vtx0 = new Vertex(array('x' => $vec->getX(), 'y' => $vec->getY(), 'z' => $vec->getZ(), 'w' => 1.0)); $this->_M = array('vtcX' => $vtcX, 'vtcY' => $vtcY, 'vtcZ' => $vtcZ, 'vtx0' => $vtx0); }
/** * construct a vector from this point instance. */ public function getVector() { $vector = new Vector(); $vector->x($this->x()); $vector->y($this->y()); $vector->z($this->z()); return $vector; }
/** * Умножение вектора на скаляр * * @param Vector $vector_a Вектор * @param Scalar $scalar_a Скаляр * * @return Vector */ public static function multiply_on_scalar(Vector $vector_a, Scalar $scalar_b) { // Создаем итоговый вектор $vector_res = new Vector($vector_a->get_size()); // Производим умножение вектора на скаляр for ($i = 0; $i < $vector_a->get_size(); $i++) $vector_res->set_item($i, Scalar_Helper::multiply($vector_a->get_item($i), $scalar_b)); // Возвращаем результат return $vector_res; }
function __construct() { global $meth; $args = func_get_args(); $vector = new Vector($args); $this->method_body = $vector->pop(); foreach ($vector as $arg) { $vector->data[$vector->key()] = preg_match("/^\$/", $arg) ? $arg : '$' . $arg; } $this->method_arguments = join(",", $vector->data); $this->method_name = create_function($this->method_arguments, $this->method_body); }
/** * Constructor: Get the qr decomposition of the given matrix using householder transformations. * The single householder matrizes are stores within the matrix. * * @param matrix matrix to get the composition of */ public function __construct(&$matrix) { new Assertion($matrix instanceof Matrix, 'Given matrix not of class Matrix.'); $this->_tau = new Vector($matrix->columns()); $this->_matrix = $matrix->copy(); for ($j = 0; $j < $this->_matrix->columns(); $j++) { $norm = 0.0; for ($i = $j; $i < $this->_matrix->rows(); $i++) { $norm += pow($this->_matrix->get($i, $j), 2); } $norm = sqrt($norm); $sign = 1.0; if ($this->_matrix->get($j, $j) < 0) { $sign = -1.0; } $v1 = $this->_matrix->get($j, $j) + $sign * $norm; $scalar = 1; for ($i = $j + 1; $i < $this->_matrix->rows(); $i++) { $this->_matrix->set($i, $j, $this->_matrix->get($i, $j) / $v1); $scalar += pow($this->_matrix->get($i, $j), 2); } $this->_tau->set($j, 2.0 / $scalar); $w = new Vector($this->_matrix->columns()); $w->setAll(0.0); // First calculate w = v_j^T * A. for ($i = $j; $i < $this->_matrix->columns(); $i++) { $w->set($i, $this->_matrix->get($j, $i)); for ($k = $j + 1; $k < $this->_matrix->rows(); $k++) { if ($i == $j) { $w->set($i, $w->get($i) + $this->_matrix->get($k, $j) * $this->_matrix->get($k, $i) * $v1); } else { $w->set($i, $w->get($i) + $this->_matrix->get($k, $j) * $this->_matrix->get($k, $i)); } } $this->_matrix->set($j, $i, $this->_matrix->get($j, $i) - $this->_tau->get($j) * $w->get($i)); for ($k = $j + 1; $k < $this->_matrix->rows(); $k++) { if ($i > $j) { $this->_matrix->set($k, $i, $this->_matrix->get($k, $i) - $this->_tau->get($j) * $this->_matrix->get($k, $j) * $w->get($i)); } } } // Assemble v for trace. $v = new Vector($this->_matrix->rows() - $j); $v->set(0, 1); for ($i = 1; $i < $v->size(); $i++) { $v->set($i, $this->_matrix->get($i, $j)); } $this->_trace[$j] = array('v' => $v, 'matrix' => $this->_matrix->copy()); } }
public function crossProduct(Vector $other) { if ($this->length() != 3) { throw new Exception("Must be 3 dimension"); } if ($other->length() != 3) { throw new Exception("Must be 3 dimension"); } $class = get_called_class(); $result = new $class(); /* @var $result Vector */ $result->value = array($this->value[1] * $other->value[2] - $this->value[2] * $other->value[1], $this->value[2] * $other->value[0] - $this->value[0] * $other->value[2], $this->value[0] * $other->value[1] - $this->value[1] * $other->value[0]); return $result; }
/** * Умножение матрицы на вектор * * @param Matrix $matrix_a Матрица * @param Vector $vector_b Вектор * * @return Matrix */ public static function multiply_on_vector(Matrix $matrix_a, Vector $vector_b) { // Проверяем совпадение размеров if ($matrix_a->get_cols_count() != $vector_b->get_size()) throw new MathException('Размеры матрицы и вектора должны совпадать'); // Создаем итоговую матрицу $res_matrix = new Matrix($matrix_a->get_rows_count(), $matrix_a->get_cols_count()); // Производим умножение матрицы на векторами for ($i = 0; $i < $matrix_a->get_rows_count(); $i++) for ($j = 0; $j < $matrix_a->get_cols_count(); $j++) $res_matrix->set_item($i, $j, Scalar_Helper::multiply($matrix_a->get_item($i, $j), $vector_b->get_item($j))); // Возвращаем результат return $res_matrix; }
public static function fromPoints($point_a, $point_b, $point_c) { $plan = new Plan(); // n: AB x AC // u: AB // v: AC // p: OA $u = Vector::fromPoints($point_a, $point_b); $v = Vector::fromPoints($point_a, $point_c); $point = Vector::fromComponents($point_a->x(), $point_a->y(), $point_a->z()); }
static function __static() { self::$iterate = newinstance('Iterator', array(), '{ private $i= 0, $v; public function on($v) { $self= new self(); $self->v= $v; return $self; } public function current() { return $this->v[$this->i]; } public function key() { return $this->i; } public function next() { $this->i++; } public function rewind() { $this->i= 0; } public function valid() { return $this->i < sizeof($this->v); } }'); }
public function Program() { $a = array(3, 4, 5); $b = array(4, 3, 5); $c = array(-5, -12, -13); $va = new Vector($a); $vb = new Vector($b); $vc = new Vector($c); $result1 = Vector::dotProduct($va, $vb); $result2 = Vector::crossProduct($va, $vb)->getValues(); $result3 = Vector::scalarTripleProduct($va, $vb, $vc); $result4 = Vector::vectorTrippleProduct($va, $vb, $vc)->getValues(); printf("\n"); printf("A = (%0.2f, %0.2f, %0.2f)\n", $a[0], $a[1], $a[2]); printf("B = (%0.2f, %0.2f, %0.2f)\n", $b[0], $b[1], $b[2]); printf("C = (%0.2f, %0.2f, %0.2f)\n", $c[0], $c[1], $c[2]); printf("\n"); printf("A · B = %0.2f\n", $result1); printf("A × B = (%0.2f, %0.2f, %0.2f)\n", $result2[0], $result2[1], $result2[2]); printf("A · (B × C) = %0.2f\n", $result3); printf("A × (B × C) =(%0.2f, %0.2f, %0.2f)\n", $result4[0], $result4[1], $result4[2]); }
/** * @return Vector */ function keys() { $vector = new Vector(); $vector->appendAll($this->getIterator()->keys()); return $vector; }
public function vectorReadElement() { $v = new Vector(); $world = new String('world'); $v->add($world); $this->assertEquals($world, $v[0]); }
<?php use Tag\Tag; use Tag\Factory; include 'vendor/autoload.php'; $logger = new Logger('test.log'); exec('rm -f test.sqlite'); $database = new Database(':memory:', $logger); // $database = new Database('test.sqlite', $logger); $factory = new Factory($database, $logger); $collection = $factory->getCollection(); $data = json_decode(file_get_contents('groph.json'), true); $fixtures = $data['tags']; $collection->load($fixtures); $t = $collection->findFirst('programming languages'); echo $t->ref, PHP_EOL; $tag1 = $collection->findFirst('software development'); $path = Vector::create('programming languages', 'interpreted programming languages', 'javascript'); $steps = 0; $names = Vector::create(); $tag1->visitPath($path, function (Tag $tag) use(&$steps, &$names) { echo "{$tag} [" . $tag->getId() . ']', PHP_EOL; $steps++; $names->append($tag->getName()); }); echo $path->count() . ' = ' . $steps, PHP_EOL;
public function sub(Vector $rhs) { return new Vector(array('dest' => new Vertex(array('x' => $this->_x - $rhs->getX(), 'y' => $this->_y - $rhs->getY(), 'z' => $this->_z - $rhs->getZ())))); }
$vtxX = new Vertex(array('x' => 1.0, 'y' => 0.0, 'z' => 0.0)); $vtxY = new Vertex(array('x' => 0.0, 'y' => 1.0, 'z' => 0.0)); $vtxZ = new Vertex(array('x' => 0.0, 'y' => 0.0, 'z' => 1.0)); $vtcXunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxX)); $vtcYunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxY)); $vtcZunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxZ)); print $vtcXunit . PHP_EOL; print $vtcYunit . PHP_EOL; print $vtcZunit . PHP_EOL; $dest1 = new Vertex(array('x' => -12.34, 'y' => 23.45, 'z' => -34.56)); Vertex::$verbose = True; $vtc1 = new Vector(array('dest' => $dest1)); Vertex::$verbose = False; $orig2 = new Vertex(array('x' => 23.87, 'y' => -37.95, 'z' => 78.34)); $dest2 = new Vertex(array('x' => -12.34, 'y' => 23.45, 'z' => -34.56)); $vtc2 = new Vector(array('orig' => $orig2, 'dest' => $dest2)); print 'Magnitude is ' . $vtc2->magnitude() . PHP_EOL; /* $nVtc2 = $vtc2->normalize(); print( 'Normalized $vtc2 is ' . $nVtc2 . PHP_EOL ); print( 'Normalized $vtc2 magnitude is ' . $nVtc2->magnitude() . PHP_EOL ); print( '$vtc1 + $vtc2 is ' . $vtc1->add( $vtc2 ) . PHP_EOL ); print( '$vtc1 - $vtc2 is ' . $vtc1->sub( $vtc2 ) . PHP_EOL ); print( 'opposite of $vtc1 is ' . $vtc1->opposite() . PHP_EOL ); print( 'scalar product of $vtc1 and 42 is ' . $vtc1->scalarProduct( 42 ) . PHP_EOL ); print( 'dot product of $vtc1 and $vtc2 is ' . $vtc1->dotProduct( $vtc2 ) . PHP_EOL ); print( 'cross product of $vtc1 and $vtc2 is ' . $vtc1->crossProduct( $vtc2 ) . PHP_EOL ); print( 'cross product of $vtcXunit and $vtcYunit is ' . $vtcXunit->crossProduct( $vtcYunit ) . 'aka $vtcZunit' . PHP_EOL ); print( 'cosinus of angle between $vtc1 and $vtc2 is ' . $vtc1->cos( $vtc2 ) . PHP_EOL ); print( 'cosinus of angle between $vtcXunit and $vtcYunit is ' . $vtcXunit->cos( $vtcYunit ) . PHP_EOL );
public function solve() { if (!Matrix_Helper::is_symmetric_matrix($this->matrix_a)) throw new MathException('Матрица не симметическая. Использовать метод квадратного корня нецелесообразно'); if ($this->matrix_a->get_rows_count() != $this->vector_b->get_size()) throw new MathException('Размеры матрицы и ветора должны быть одинаковыми'); $n = $this->matrix_a->get_rows_count(); // Находим D, S $matrix_d = new Matrix($n, $n); $matrix_s = new Matrix($n, $n); for ($i = 0; $i < $n; $i++) { $sum = new Scalar(Scalar::ZERO); for ($p = 0; $p <= $i-1; $p++) $sum = Scalar_Helper::sum( $sum, Scalar_Helper::multiply( Scalar_Helper::sqr( $matrix_s->get_item($p, $i) ), $matrix_d->get_item($p, $p) ) ); // d[i, i] $matrix_d->set_item($i, $i, Scalar_Helper::sign( Scalar_Helper::sub( $this->matrix_a->get_item($i, $i), $sum ) ) ); // s[i, i] $matrix_s->set_item($i, $i, Scalar_Helper::sqrt( Scalar_Helper::abs( Scalar_Helper::sub( $this->matrix_a->get_item($i, $i), $sum ) ) ) ); // s[i, j] for ($j = $i+1; $j < $n; $j++) { $sum = new Scalar(Scalar::ZERO); for ($p = 0; $p <= $i-1; $p++) $sum = Scalar_Helper::sum( $sum, Scalar_Helper::multiply( Scalar_Helper::multiply( $matrix_s->get_item($p, $i), $matrix_d->get_item($p, $p) ), $matrix_s->get_item($p, $j) ) ); // s[i, j] $matrix_s->set_item($i, $j, Scalar_Helper::division( Scalar_Helper::sub( $this->matrix_a->get_item($i, $j), $sum ), Scalar_Helper::multiply( $matrix_d->get_item($i, $i), $matrix_s->get_item($i, $i) ) ) ); } } $matrix_b = Matrix_Helper::multiply( Matrix_Helper::transpose($matrix_s), $matrix_d ); $vector_y = new Vector($n); for ($k = 0; $k < $n; $k++) { $sum = new Scalar(Scalar::ZERO); for ($s = 0; $s <= $k-1; $s++) $sum = Scalar_Helper::sum( $sum, Scalar_Helper::multiply( $matrix_b->get_item($k, $s), $vector_y->get_item($s) ) ); $vector_y->set_item($k, Scalar_Helper::division( Scalar_Helper::sub( $this->vector_b->get_item($k), $sum ), $matrix_b->get_item($k, $k) )); } $this->vector_x = new Vector($n); for ($k = $n-1; $k >= 0; $k--) { $sum = new Scalar(Scalar::ZERO); for ($p = $k+1; $p < $n; $p++) $sum = Scalar_Helper::sum( $sum, Scalar_Helper::multiply( $matrix_s->get_item($k, $p), $this->vector_x->get_item($p) ) ); $this->vector_x->set_item($k, Scalar_Helper::division( Scalar_Helper::sub( $vector_y->get_item($k), $sum ), $matrix_s->get_item($k, $k) )); } }
/** * Axiom: |projᵇA⊥ ⋅ perpᵇA| = |projᵇA| |perpᵇA| * Absolute value of proj and perp dot product equals product of their lengths. * @dataProvider dataProviderForProjPerp */ public function testProjPerpPerpDotProductEqualsProductOfLengths(array $A, array $B) { $A = new Vector($A); $B = new Vector($B); $projᵇA = $A->projection($B); $projᵇA⊥ = $A->projection($B)->perpendicular(); $perpᵇA = $A->perp($B); $projᵇA⊥⋅perpᵇA = abs($projᵇA⊥->dotProduct($perpᵇA)); $│projᵇA│ = $projᵇA->length(); $│perpᵇA│ = $perpᵇA->length(); $this->assertEquals($projᵇA⊥⋅perpᵇA, $│projᵇA│ * $│perpᵇA│); }
function common_items(&$vector) { // for compatibility, we don't use pass by reference //$keys = array_intersect_key(&$this->values, &$vector->values); $keys = array_intersect_key($this->values, $vector->values); if (count($keys) == 0) { return NULL; } $array_a = array(); $array_b = array(); foreach ($keys as $key => $value) { $array_a[] = $this->values[$key]; $array_b[] = $vector->values[$key]; } $subset = array(); $subset[] = Vector::wrap('RealVector', $array_a); $subset[] = Vector::wrap('RealVector', $array_b); return $subset; }
/** * @dataProvider dataProviderForMaxNorm */ public function testMaxNorm(array $A, $maxnorm) { $A = new Vector($A); $this->assertEquals($maxnorm, $A->maxNorm(), '', 0.0001); }
function draw_straight($image, &$curvepoints, $widths, $outlinecolour, $fillcolours, $linkname, &$map, $q2_percent = 50, $unidirectional = FALSE) { $totaldistance = $curvepoints[count($curvepoints) - 1][DISTANCE]; if ($unidirectional) { $halfway = $totaldistance; $dirs = array(OUT); $q2_percent = 100; $halfway = $totaldistance * ($q2_percent / 100); list($halfway_x, $halfway_y, $halfwayindex) = find_distance_coords($curvepoints, $halfway); $spine[OUT] = $curvepoints; } else { // we'll split the spine in half here. # $q2_percent = 50; $halfway = $totaldistance * ($q2_percent / 100); $dirs = array(OUT, IN); # $dirs = array(IN); list($halfway_x, $halfway_y, $halfwayindex) = find_distance_coords($curvepoints, $halfway); # print "Midpoint is: $totaldistance $halfway $halfwayindex $halfway_x,$halfway_y\n"; $spine[OUT] = array(); $spine[IN] = array(); $npoints = count($curvepoints) - 1; for ($i = 0; $i <= $halfwayindex; $i++) { $spine[OUT][] = $curvepoints[$i]; } // finally, add the actual midpoint $spine[OUT][] = array($halfway_x, $halfway_y, $halfway); // and then from the end to the middle for the other arrow for ($i = $npoints; $i > $halfwayindex; $i--) { // copy the original spine, but reversing the distance calculation $spine[IN][] = array($curvepoints[$i][X], $curvepoints[$i][Y], $totaldistance - $curvepoints[$i][DISTANCE]); } // finally, add the actual midpoint $spine[IN][] = array($halfway_x, $halfway_y, $totaldistance - $halfway); } # wm_draw_marker_box($image,$map->selected, $halfway_x, $halfway_y ); // now we have two seperate spines, with distances, so that the arrowhead is the end of each. // (or one, if it's unidir) // so we can loop along the spine for each one as a seperate entity // we calculate the arrow size up here, so that we can decide on the // minimum length for a link. The arrowheads are the limiting factor. list($arrowsize[IN], $arrowwidth[IN]) = calc_arrowsize($widths[IN], $map, $linkname); list($arrowsize[OUT], $arrowwidth[OUT]) = calc_arrowsize($widths[OUT], $map, $linkname); // the 1.2 here is empirical. It ought to be 1 in theory. // in practice, a link this short is useless anyway, especially with bwlabels. $minimumlength = 1.2 * ($arrowsize[IN] + $arrowsize[OUT]); foreach ($dirs as $dir) { # draw_spine($image, $spine[$dir],$map->selected); #draw_spine_chain($image, $spine[$dir],$map->selected,3); #print "=================\n$linkname/$dir\n"; #dump_spine($spine[$dir]); $n = count($spine[$dir]) - 1; $l = $spine[$dir][$n][DISTANCE]; #print "L=$l N=$n\n"; // loop increment, start point, width, labelpos, fillcolour, outlinecolour, commentpos $arrowsettings = array(+1, 0, $widths[$dir], 0, $fillcolours[$dir], $outlinecolour, 5); # print "Line is $n points to a distance of $l\n"; if ($l < $minimumlength) { wm_warn("Skipping too-short line.\n"); } else { $arrow_d = $l - $arrowsize[$dir]; # print "LENGTHS $l $arrow_d ".$arrowsize[$dir]."\n"; list($pre_mid_x, $pre_mid_y, $pre_midindex) = find_distance_coords($spine[$dir], $arrow_d); # print "POS $pre_mid_x,$pre_mid_y $pre_midindex\n"; $out = array_slice($spine[$dir], 0, $pre_midindex); $out[] = array($pre_mid_x, $pre_mid_y, $arrow_d); # wm_draw_marker_diamond($image, $map->selected, $pre_mid_x, $pre_mid_y, 5); # imagearc($image,$pre_mid_x, $pre_mid_y ,15,15,0,360,$map->selected); # imagearc($image,$spine[$dir][$pre_midindex+1][X],$spine[$dir][$pre_midindex+1][Y],20,20,0,360,$map->selected); # imagearc($image,$spine[$dir][$pre_midindex][X],$spine[$dir][$pre_midindex][Y],20,20,0,360,$map->selected); #imagearc($image,$pre_mid_x,$pre_mid_y,20,20,0,360,$map->selected); #imagearc($image,$spine[$dir][$pre_midindex][X],$spine[$dir][$pre_midindex][Y],12,12,0,360,$map->selected); $spine[$dir] = $out; $adx = $halfway_x - $pre_mid_x; $ady = $halfway_y - $pre_mid_y; $ll = sqrt($adx * $adx + $ady * $ady); $anx = $ady / $ll; $any = -$adx / $ll; $ax1 = $pre_mid_x + $widths[$dir] * $anx; $ay1 = $pre_mid_y + $widths[$dir] * $any; $ax2 = $pre_mid_x + $arrowwidth[$dir] * $anx; $ay2 = $pre_mid_y + $arrowwidth[$dir] * $any; $ax3 = $halfway_x; $ay3 = $halfway_y; $ax5 = $pre_mid_x - $widths[$dir] * $anx; $ay5 = $pre_mid_y - $widths[$dir] * $any; $ax4 = $pre_mid_x - $arrowwidth[$dir] * $anx; $ay4 = $pre_mid_y - $arrowwidth[$dir] * $any; # draw_spine($image,$spine[$dir],$map->selected); $simple = simplify_spine($spine[$dir]); $newn = count($simple); # draw_spine($image,$simple,$map->selected); # print "Simplified to $newn points\n"; # if($draw_skeleton) draw_spine_chain($im,$simple,$blue, 12); # draw_spine_chain($image,$simple,$map->selected, 12); # draw_spine_chain($image,$spine[$dir],$map->selected, 10); # draw_spine_chain($image,$simple,$map->selected, 12); # draw_spine($image,$simple,$map->selected); // now do the actual drawing.... $numpoints = 0; $numrpoints = 0; $finalpoints = array(); $reversepoints = array(); $finalpoints[] = $simple[0][X]; $finalpoints[] = $simple[0][Y]; $numpoints++; $reversepoints[] = $simple[0][X]; $reversepoints[] = $simple[0][Y]; $numrpoints++; // before the main loop, add in the jump out to the corners // if this is the first step, then we need to go from the middle to the outside edge first // ( the loop may not run, but these corners are required) $i = 0; $v1 = new Vector($simple[$i + 1][X] - $simple[$i][X], $simple[$i + 1][Y] - $simple[$i][Y]); $n1 = $v1->get_normal(); $finalpoints[] = $simple[$i][X] + $n1->dx * $widths[$dir]; $finalpoints[] = $simple[$i][Y] + $n1->dy * $widths[$dir]; $numpoints++; $reversepoints[] = $simple[$i][X] - $n1->dx * $widths[$dir]; $reversepoints[] = $simple[$i][Y] - $n1->dy * $widths[$dir]; $numrpoints++; $max_start = count($simple) - 2; # print "max_start is $max_start\n"; for ($i = 0; $i < $max_start; $i++) { $v1 = new Vector($simple[$i + 1][X] - $simple[$i][X], $simple[$i + 1][Y] - $simple[$i][Y]); $v2 = new Vector($simple[$i + 2][X] - $simple[$i + 1][X], $simple[$i + 2][Y] - $simple[$i + 1][Y]); $n1 = $v1->get_normal(); $n2 = $v2->get_normal(); $capping = FALSE; // figure out the angle between the lines - for very sharp turns, we should do something special // (actually, their normals, but the angle is the same and we need the normals later) $angle = rad2deg(atan2($n2->dy, $n2->dx) - atan2($n1->dy, $n1->dx)); if ($angle > 180) { $angle -= 360; } if ($angle < -180) { $angle += 360; } if (abs($angle) > 169) { $capping = TRUE; # print "Would cap. ($angle)\n"; } // $capping = FALSE; // override that for now // now figure out the geometry for where the next corners are list($xi1, $yi1) = line_crossing($simple[$i][X] + $n1->dx * $widths[$dir], $simple[$i][Y] + $n1->dy * $widths[$dir], $simple[$i + 1][X] + $n1->dx * $widths[$dir], $simple[$i + 1][Y] + $n1->dy * $widths[$dir], $simple[$i + 1][X] + $n2->dx * $widths[$dir], $simple[$i + 1][Y] + $n2->dy * $widths[$dir], $simple[$i + 2][X] + $n2->dx * $widths[$dir], $simple[$i + 2][Y] + $n2->dy * $widths[$dir]); list($xi2, $yi2) = line_crossing($simple[$i][X] - $n1->dx * $widths[$dir], $simple[$i][Y] - $n1->dy * $widths[$dir], $simple[$i + 1][X] - $n1->dx * $widths[$dir], $simple[$i + 1][Y] - $n1->dy * $widths[$dir], $simple[$i + 1][X] - $n2->dx * $widths[$dir], $simple[$i + 1][Y] - $n2->dy * $widths[$dir], $simple[$i + 2][X] - $n2->dx * $widths[$dir], $simple[$i + 2][Y] - $n2->dy * $widths[$dir]); if (!$capping) { $finalpoints[] = $xi1; $finalpoints[] = $yi1; $numpoints++; $reversepoints[] = $xi2; $reversepoints[] = $yi2; $numrpoints++; } else { // in here, we need to decide which is the 'outside' of the corner, // because that's what we flatten. The inside of the corner is left alone. // - depending on the relative angle between the two segments, it could // be either one of these points. list($xi3, $yi3) = line_crossing($simple[$i][X] + $n1->dx * $widths[$dir], $simple[$i][Y] + $n1->dy * $widths[$dir], $simple[$i + 1][X] + $n1->dx * $widths[$dir], $simple[$i + 1][Y] + $n1->dy * $widths[$dir], $simple[$i + 1][X] - $n2->dx * $widths[$dir], $simple[$i + 1][Y] - $n2->dy * $widths[$dir], $simple[$i + 2][X] - $n2->dx * $widths[$dir], $simple[$i + 2][Y] - $n2->dy * $widths[$dir]); list($xi4, $yi4) = line_crossing($simple[$i][X] - $n1->dx * $widths[$dir], $simple[$i][Y] - $n1->dy * $widths[$dir], $simple[$i + 1][X] - $n1->dx * $widths[$dir], $simple[$i + 1][Y] - $n1->dy * $widths[$dir], $simple[$i + 1][X] + $n2->dx * $widths[$dir], $simple[$i + 1][Y] + $n2->dy * $widths[$dir], $simple[$i + 2][X] + $n2->dx * $widths[$dir], $simple[$i + 2][Y] + $n2->dy * $widths[$dir]); if ($angle < 0) { $finalpoints[] = $xi3; $finalpoints[] = $yi3; $numpoints++; $finalpoints[] = $xi4; $finalpoints[] = $yi4; $numpoints++; $reversepoints[] = $xi2; $reversepoints[] = $yi2; $numrpoints++; } else { $reversepoints[] = $xi4; $reversepoints[] = $yi4; $numrpoints++; $reversepoints[] = $xi3; $reversepoints[] = $yi3; $numrpoints++; $finalpoints[] = $xi1; $finalpoints[] = $yi1; $numpoints++; } } } // at this end, we add the arrowhead $finalpoints[] = $ax1; $finalpoints[] = $ay1; $finalpoints[] = $ax2; $finalpoints[] = $ay2; $finalpoints[] = $ax3; $finalpoints[] = $ay3; $finalpoints[] = $ax4; $finalpoints[] = $ay4; $finalpoints[] = $ax5; $finalpoints[] = $ay5; $numpoints += 5; // combine the forwards and backwards paths, to make a complete loop for ($i = ($numrpoints - 1) * 2; $i >= 0; $i -= 2) { $x = $reversepoints[$i]; $y = $reversepoints[$i + 1]; $finalpoints[] = $x; $finalpoints[] = $y; $numpoints++; } // $finalpoints[] contains a complete outline of the line at this stage if (!is_null($fillcolours[$dir])) { wimagefilledpolygon($image, $finalpoints, count($finalpoints) / 2, $arrowsettings[4]); } else { wm_debug("Not drawing {$linkname} ({$dir}) fill because there is no fill colour\n"); } $areaname = "LINK:L" . $map->links[$linkname]->id . ":{$dir}"; $map->imap->addArea("Polygon", $areaname, '', $finalpoints); wm_debug("Adding Poly imagemap for {$areaname}\n"); if (!is_null($outlinecolour)) { wimagepolygon($image, $finalpoints, count($finalpoints) / 2, $arrowsettings[5]); } else { wm_debug("Not drawing {$linkname} ({$dir}) outline because there is no outline colour\n"); } } } }
/** * @dataProvider dataProviderForDirectProduct */ public function testDirectProduct(array $A, array $B, array $expected) { $A = new Vector($A); $B = new Vector($B); $AB = $A->directProduct($B); $expected = new Matrix($expected); $this->assertEquals($expected->getMatrix(), $AB->getMatrix()); }
/** *Checks if a class has a method named $method (boolean mode) * *@param String $name class name *@param String $method method to be checked *@param Mixed $filters, a set of filters to check for class attributes such as public, * private, abstract, final, etc. You can specify a string such as "abstract" * or you can specify a set of filters in an Array like structure * for instance, specifying $filters to Array('static','public') * would check for a method named $method that is both static AND public. * Note: This is affected according to the mode you specify, * which is by default "all", meaning that every filter you specify * must be present in the given method. * *@param String $mode mode can be any of "all", "any" or "some". The default mode is "all" * * "all" mode: the method must contain ALL and *ONLY* the parameters that you * specify in the $filters argument. If it contains extra attributes it will be * considered as invalid. * * "any" mode: the method must contain at least ONE of the parameters specified * in $filters. Extra attributes don't matter. * * "some" mode: the method must contain all of the attributes specified in * $filters, but if it contains any other attribute it's still considered * valid. * * is necessary to exist in the given method for it to be TRUE. * * *@param String $msg Exception message if any *@param Int $exCode Exception code * *@return Int -6 if $filters are not null and an empty string was entered *@return Int -7 if $filters are an array but the array elements are not strings or it's empty *@return Int -8 ONLY if specified filters are empty *@return Int -9 ONLY if an invalid filter was specified *@return Int -10 Method doesn't exists *@return boolean TRUE if the class has a method named $method *@return boolean FALSE if the class doesn't has a method named $method * *@see self::parameterValidation for other return values */ public static function hasMethod($name, $method, $filters = NULL, $mode = "all") { $validModes = array('all', 'any', 'some'); $stdValidation = self::parameterValidation($name, $method); //something went wrong if (!($stdValidation === TRUE)) { return $stdValidation; } $rc = new \ReflectionClass($name); //If no filters where specified, then check if the given class has a given method //and thats it! We don't need further checking if the user hasn't specified any filters. if (is_null($filters)) { return $rc->hasMethod($method); } //Check if specified $filters are invalid if (is_string($filters)) { if (String::isEmpty($filters)) { return -6; } } //If it's an array but the array is not made of strings if (!is_null(Vector::isArray($filters)) && !Vector::isMadeOfStrings($filters)) { return -7; } if (is_string($filters)) { $filters = array($filters); } $mode = strtolower($mode); //Specified $mode is invalid (not one of all,any or some) if (String::isEmpty($mode) || !in_array($mode, $validModes)) { return -7; } //Check if entered filters are not empty if (empty($filters)) { return -8; } //Check if entered filters are valid foreach ($filters as $filter) { $filter = sprintf('IS_%s', strtoupper($filter)); //If an invalid filter is detected, then return -9 if (!self::hasConstant('\\ReflectionMethod', $filter)) { return -9; } } //Fetch all class methods $methods = $rc->getMethods(); $found = FALSE; foreach ($methods as $m) { //If the current method is not equal to the selected one then continue //with the next element in the array if ($m->name !== $method) { continue; } $found = TRUE; break; } //Method not found if (!$found) { return -8; } //Get all method modifiers (public, abstract, private, final, etc) $modifiers = \Reflection::getModifierNames($m->getModifiers()); switch ($mode) { case "any": foreach ($filters as $f) { if (in_array($f, $modifiers)) { return TRUE; } } return FALSE; break; case "some": $count = 0; foreach ($modifiers as $mod) { if (in_array($mod, $filters)) { $count++; } } return $count == sizeof($filters); break; case "all": foreach ($modifiers as $mod) { if (!in_array($mod, $filters)) { return FALSE; } } return TRUE; break; } }
/** * Creates an empty Stack * * @access public */ public function __construct() { parent::__construct(); $this->elementCount = count($this->list); }
public function testFindingTagsByPath() { $path = Vector::create('task runners', 'gulp'); $results = $this->coll->findByPath($path); $this->assertEquals(3, $results->count()); foreach ($results as $tag) { $this->assertNotNull($tag->getParent()); $this->assertTrue($tag->getName()->matches('gulp')); $this->assertTrue($tag->getParent()->getName()->matches('task runners')); $grandpa = $tag->getParent()->getParent(); $name = $tag->getParent()->getParent()->getName(); $this->assertTrue($grandpa->getName()->matches('development tools') || $grandpa->getName()->matches('node js')); if ($grandpa->getName()->matches('node js')) { $ggp = $grandpa->getParent(); $this->assertTrue($ggp->getName()->matches('javascript') || $ggp->getName()->matches('asynchronous event-driven programming')); } else { $this->assertEquals($grandpa, $this->coll->findFirst('development tools')); } } }
Vertex::$verbose = False; print Vector::doc(); Vector::$verbose = True; $vtxO = new Vertex(array('x' => 0.0, 'y' => 0.0, 'z' => 0.0)); $vtxX = new Vertex(array('x' => 1.0, 'y' => 0.0, 'z' => 0.0)); $vtxY = new Vertex(array('x' => 0.0, 'y' => 1.0, 'z' => 0.0)); $vtxZ = new Vertex(array('x' => 0.0, 'y' => 0.0, 'z' => 1.0)); $vtcXunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxX)); $vtcYunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxY)); $vtcZunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxZ)); print $vtcXunit . PHP_EOL; print $vtcYunit . PHP_EOL; print $vtcZunit . PHP_EOL; $dest1 = new Vertex(array('x' => -12.34, 'y' => 23.45, 'z' => -34.56)); Vertex::$verbose = True; $vtc1 = new Vector(array('dest' => $dest1)); Vertex::$verbose = False; $orig2 = new Vertex(array('x' => 23.87, 'y' => -37.95, 'z' => 78.34)); $dest2 = new Vertex(array('x' => -12.34, 'y' => 23.45, 'z' => -34.56)); $vtc2 = new Vector(array('orig' => $orig2, 'dest' => $dest2)); print 'Magnitude is ' . $vtc2->magnitude() . PHP_EOL; $nVtc2 = $vtc2->normalize(); print 'Normalized $vtc2 is ' . $nVtc2 . PHP_EOL; print 'Normalized $vtc2 magnitude is ' . $nVtc2->magnitude() . PHP_EOL; print '$vtc1 + $vtc2 is ' . $vtc1->add($vtc2) . PHP_EOL; print '$vtc1 - $vtc2 is ' . $vtc1->sub($vtc2) . PHP_EOL; print 'opposite of $vtc1 is ' . $vtc1->opposite() . PHP_EOL; print 'scalar product of $vtc1 and 42 is ' . $vtc1->scalarProduct(42) . PHP_EOL; print 'dot product of $vtc1 and $vtc2 is ' . $vtc1->dotProduct($vtc2) . PHP_EOL; print 'cross product of $vtc1 and $vtc2 is ' . $vtc1->crossProduct($vtc2) . PHP_EOL; print 'cross product of $vtcXunit and $vtcYunit is ' . $vtcXunit->crossProduct($vtcYunit) . 'aka $vtcZunit' . PHP_EOL;
function export($file) { global $tagCollection, $resCollection; $tags = array(); foreach ($tagCollection->findLike(array('')) as $tag) { $parentPath = $tag->getParent() ? $tag->getParent()->getUniquePath()->implode(':') : ''; $tags[] = array((string) $tag->getName() => $parentPath); } $resources = array(); foreach ($resCollection->findLike(array('')) as $res) { $resources[$res->getLink()] = array($res->getTitle(), $res->getTags()->toStringsArray()); } file_put_contents($file, Vector::create(array('tags' => $tags, 'resources' => $resources))->toJson()); }
/** * Multiplay given matrix by given vector. * * @param matrix matrix * @param vector vector * @return vector product matrix*vector */ public static function operate($matrix, $vector) { // First check dimensions. new Assertion($matrix instanceof Matrix, 'Given matrix not of class Matrix.'); new Assertion($vector instanceof Vector, 'Given vector not of class Vector.'); new Assertion($matrix->columns() == $vector->size(), 'Given dimensions are not compatible.'); $result = new Vector($matrix->rows()); $result->setAll(0.0); for ($i = 0; $i < $matrix->rows(); $i++) { for ($j = 0; $j < $matrix->columns(); $j++) { $result->set($i, $result->get($i) + $matrix->get($i, $j) * $vector->get($j)); } } return $result; }
/** * @dataProvider dataProviderForAsRowMatrix */ public function testAsRowMatrix(array $A, array $R) { $A = new Vector($A); $R = new Matrix($R); $M = $A->asRowMatrix(); $this->assertEquals($R, $M); }