Skip to content

Commit d0d112f

Browse files
committed
Add Matrix columnSubtractScalar.
1 parent d698f03 commit d0d112f

File tree

4 files changed

+113
-6
lines changed

4 files changed

+113
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New Features
66
* Matrix `columnAddScalar`
77
* Matrix `columnSubtract`
8+
* Matrix `columnSubtractScalar`
89

910
## v2.12.0 - 2025-10-17
1011

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,13 @@ $R = $A->rowSubtractScalar($mᵢ, $k); // Subtract k from each item of row mᵢ
503503
// Column operations
504504
[$nᵢ, $nⱼ, $k] = [1, 2, 5];
505505
$R = $A->columnInterchange($nᵢ, $nⱼ);
506-
$R = $A->columnExclude($nᵢ); // Exclude column $nᵢ
507-
$R = $A->columnMultiply($nᵢ, $k); // Multiply column nᵢ by k
508-
$R = $A->columnAdd($nᵢ, $nⱼ, $k); // Add k * column nᵢ to column nⱼ
509-
$R = $A->columnAddScalar($n, $k); // Add k to each item in column nⱼ
510-
$R = $A->columnAddVector($nᵢ, $V); // Add Vector V to column nᵢ
511-
$R = $A->columnSubtract($nᵢ, $nⱼ, $k); // Subtract k * column nᵢ from row nⱼ
506+
$R = $A->columnExclude($nᵢ); // Exclude column $nᵢ
507+
$R = $A->columnMultiply($nᵢ, $k); // Multiply column nᵢ by k
508+
$R = $A->columnAdd($nᵢ, $nⱼ, $k); // Add k * column nᵢ to column nⱼ
509+
$R = $A->columnAddScalar($n, $k); // Add k to each item in column nⱼ
510+
$R = $A->columnAddVector($nᵢ, $V); // Add Vector V to column nᵢ
511+
$R = $A->columnSubtract($nᵢ, $nⱼ, $k); // Subtract k * column nᵢ from row nⱼ
512+
$R = $A->columnSubtractScalar($nᵢ, $k); // Subtract k from each item of column nᵢ
512513

513514
// Matrix augmentations - return a new Matrix
514515
$⟮A∣B⟯ = $A->augment($B); // Augment on the right - standard augmentation

src/LinearAlgebra/NumericMatrix.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,6 +2597,7 @@ public function rowSubtractScalar(int $mᵢ, float $k): NumericMatrix
25972597
* - columnAddScalar
25982598
* - columnAddVector
25992599
* - columnSubtract
2600+
* - columnSubtractScalar
26002601
**************************************************************************/
26012602

26022603
/**
@@ -2746,6 +2747,35 @@ public function columnSubtract(int $nᵢ, int $nⱼ, float $k): NumericMatrix
27462747
return MatrixFactory::createNumeric($R, $this->ε);
27472748
}
27482749

2750+
/**
2751+
* Subtract a scalar k to each item of a column
2752+
*
2753+
* Each element of Column nᵢ will have k subtracted from it
2754+
*
2755+
* @param int $nᵢ Column to subtract k from
2756+
* @param float $k scalar
2757+
*
2758+
* @return NumericMatrix
2759+
*
2760+
* @throws Exception\MatrixException if column to subtract does not exist
2761+
* @throws Exception\IncorrectTypeException
2762+
*/
2763+
public function columnSubtractScalar(int $nᵢ, float $k): NumericMatrix
2764+
{
2765+
if ($nᵢ >= $this->n) {
2766+
throw new Exception\MatrixException('Column to subtract does not exist');
2767+
}
2768+
2769+
$m = $this->m;
2770+
$R = $this->A;
2771+
2772+
for ($i = 0; $i < $m; $i++) {
2773+
$R[$i][$nᵢ] -= $k;
2774+
}
2775+
2776+
return MatrixFactory::createNumeric($R, $this->ε);
2777+
}
2778+
27492779
/**************************************************************************
27502780
* MATRIX REDUCTIONS - Return a Matrix in a reduced form
27512781
* - ref (row echelon form)

tests/LinearAlgebra/Matrix/Numeric/MatrixColumnOperationsTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,79 @@ public function testColumnSubtractExceptionColumnGreaterThanN()
615615
// When
616616
$A->columnSubtract(4, 5, 2);
617617
}
618+
619+
/**
620+
* @test columnSubtractScalar
621+
* @dataProvider dataProviderForColumnSubtractScalar
622+
* @param array $A
623+
* @param int $nᵢ
624+
* @param float $k
625+
* @param array $expectedMatrix
626+
* @throws \Exception
627+
*/
628+
public function testColumnSubtractScalar(array $A, int $nᵢ, float $k, array $expectedMatrix)
629+
{
630+
// Given
631+
$A = MatrixFactory::create($A);
632+
$expectedMatrix = MatrixFactory::create($expectedMatrix);
633+
634+
// When
635+
$R = $A->columnSubtractScalar($nᵢ, $k);
636+
637+
// Then
638+
$this->assertEqualsWithDelta($expectedMatrix, $R, 0.00001);
639+
}
640+
641+
/**
642+
* @return array
643+
*/
644+
public function dataProviderForColumnSubtractScalar(): array
645+
{
646+
return [
647+
[
648+
[
649+
[6, 7, 8],
650+
[7, 8, 9],
651+
[8, 9, 10],
652+
], 0, 5,
653+
[
654+
[1, 7, 8],
655+
[2, 8, 9],
656+
[3, 9, 10],
657+
],
658+
],
659+
[
660+
[
661+
[6, 7, 8],
662+
[7, 8, 9],
663+
[8, 9, 10],
664+
], 0, 5.2,
665+
[
666+
[0.8, 7, 8],
667+
[1.8, 8, 9],
668+
[2.8, 9, 10],
669+
],
670+
],
671+
];
672+
}
673+
674+
/**
675+
* @test columnSubtractScalar column greater than n
676+
* @throws \Exception
677+
*/
678+
public function testColumnSubtractScalarExceptionColumnGreaterThanN()
679+
{
680+
// Given
681+
$A = MatrixFactory::create([
682+
[1, 2, 3],
683+
[2, 3, 4],
684+
[3, 4, 5],
685+
]);
686+
687+
// Then
688+
$this->expectException(Exception\MatrixException::class);
689+
690+
// When
691+
$A->columnSubtractScalar(4, 5);
692+
}
618693
}

0 commit comments

Comments
 (0)