Skip to content

Commit cd391c0

Browse files
committed
Add Matrix columnDivide.
1 parent d0d112f commit cd391c0

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Matrix `columnAddScalar`
77
* Matrix `columnSubtract`
88
* Matrix `columnSubtractScalar`
9+
* Matrix `columnDivide`
910

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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ $R = $A->rowSubtractScalar($mᵢ, $k); // Subtract k from each item of row mᵢ
505505
$R = $A->columnInterchange($nᵢ, $nⱼ);
506506
$R = $A->columnExclude($nᵢ); // Exclude column $nᵢ
507507
$R = $A->columnMultiply($nᵢ, $k); // Multiply column nᵢ by k
508+
$R = $A->columnDivide($nᵢ, $k); // Divide column nᵢ by k
508509
$R = $A->columnAdd($nᵢ, $nⱼ, $k); // Add k * column nᵢ to column nⱼ
509510
$R = $A->columnAddScalar($n, $k); // Add k to each item in column nⱼ
510511
$R = $A->columnAddVector($nᵢ, $V); // Add Vector V to column nᵢ

src/LinearAlgebra/NumericMatrix.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,7 @@ public function rowSubtractScalar(int $mᵢ, float $k): NumericMatrix
25932593
/**************************************************************************
25942594
* COLUMN OPERATIONS - Return a Matrix
25952595
* - columnMultiply
2596+
* - columnDivide
25962597
* - columnAdd
25972598
* - columnAddScalar
25982599
* - columnAddVector
@@ -2629,6 +2630,39 @@ public function columnMultiply(int $nᵢ, float $k): NumericMatrix
26292630
return MatrixFactory::createNumeric($R, $this->ε);
26302631
}
26312632

2633+
/**
2634+
* Divide a column by a divisor k
2635+
*
2636+
* Each element of Column nᵢ will be divided by k
2637+
*
2638+
* @param int $nᵢ Column to divide
2639+
* @param float $k divisor
2640+
*
2641+
* @return NumericMatrix
2642+
*
2643+
* @throws Exception\MatrixException if column to divide does not exist
2644+
* @throws Exception\BadParameterException if k is 0
2645+
* @throws Exception\IncorrectTypeException
2646+
*/
2647+
public function columnDivide(int $nᵢ, float $k): NumericMatrix
2648+
{
2649+
if ($nᵢ >= $this->n) {
2650+
throw new Exception\MatrixException('Column to divide does not exist');
2651+
}
2652+
if ($k == 0) {
2653+
throw new Exception\BadParameterException('Divisor k must not be 0');
2654+
}
2655+
2656+
$m = $this->m;
2657+
$R = $this->A;
2658+
2659+
for ($i = 0; $i < $m; $i++) {
2660+
$R[$i][$nᵢ] /= $k;
2661+
}
2662+
2663+
return MatrixFactory::createNumeric($R, $this->ε);
2664+
}
2665+
26322666
/**
26332667
* Add k times column nᵢ to column nⱼ
26342668
*

tests/LinearAlgebra/Matrix/Numeric/MatrixColumnOperationsTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,101 @@ public function testColumnMultiplyExceptionColumnGreaterThanN()
119119
$A->columnMultiply(4, 5);
120120
}
121121

122+
/**
123+
* @test columnDivide
124+
* @dataProvider dataProviderForColumnDivide
125+
* @param array $A
126+
* @param int $nᵢ
127+
* @param float $k
128+
* @param array $expectedMatrix
129+
* @throws \Exception
130+
*/
131+
public function testColumnDivide(array $A, int $nᵢ, float $k, array $expectedMatrix)
132+
{
133+
// Given
134+
$A = MatrixFactory::create($A);
135+
$expectedMatrix = MatrixFactory::create($expectedMatrix);
136+
137+
// When
138+
$R = $A->columnDivide($nᵢ, $k);
139+
140+
// Then
141+
$this->assertEqualsWithDelta($expectedMatrix, $R, 0.00001);
142+
}
143+
144+
/**
145+
* @return array
146+
*/
147+
public function dataProviderForColumnDivide(): array
148+
{
149+
return [
150+
[
151+
[
152+
[2, 3, 4],
153+
[4, 6, 8],
154+
[8, 9, 10],
155+
], 0, 2,
156+
[
157+
[1, 3, 4],
158+
[2, 6, 8],
159+
[4, 9, 10],
160+
]
161+
],
162+
[
163+
[
164+
[2, 3, 4],
165+
[4, 6, 8],
166+
[8, 9, 10],
167+
], 0, 2.1,
168+
[
169+
[0.952380952380952, 3, 4],
170+
[1.904761904761905, 6, 8],
171+
[3.80952380952381, 9, 10],
172+
]
173+
],
174+
];
175+
}
176+
177+
/**
178+
* @test columnDivide column greater than n
179+
* @throws \Exception
180+
*/
181+
public function testColumnDivideExceptionColumnGreaterThanN()
182+
{
183+
// Given
184+
$A = MatrixFactory::create([
185+
[1, 2, 3],
186+
[2, 3, 4],
187+
[3, 4, 5],
188+
]);
189+
190+
// Then
191+
$this->expectException(Exception\MatrixException::class);
192+
193+
// When
194+
$A->columnDivide(4, 5);
195+
}
196+
197+
/**
198+
* @test columnDivide K is zero
199+
* @throws \Exception
200+
*/
201+
public function testColumnDivideExceptionKIsZero()
202+
{
203+
// Given
204+
$A = MatrixFactory::create([
205+
[1, 2, 3],
206+
[2, 3, 4],
207+
[3, 4, 5],
208+
]);
209+
210+
// Then
211+
$this->expectException(Exception\BadParameterException::class);
212+
213+
// When
214+
$A->columnDivide(2, 0);
215+
}
216+
122217
/**
123218
* @test columnAdd
124219
* @dataProvider dataProviderForColumnAdd

0 commit comments

Comments
 (0)