Skip to content

Commit 4ff0c0f

Browse files
authored
Merge pull request #1 from PolyMathOrg/adding-first-norm
Add first norm and test
2 parents 5fb38e9 + f61d326 commit 4ff0c0f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Math-Vector-Tests/PMVectorTest.class.st

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@ PMVectorTest >> testVectorCumulativeSum [
217217
self assert: (w at: 3) equals: 6
218218
]
219219

220+
{ #category : #tests }
221+
PMVectorTest >> testVectorFirstNorm [
222+
223+
| v1 v2 v3 |
224+
v1 := #( 1 0 ) asPMVector.
225+
self assert: v1 firstNorm equals: 1.
226+
v2 := #( 1 1 ) asPMVector.
227+
self assert: v2 firstNorm equals: 2.
228+
v3 := #( -1 1 ) asPMVector.
229+
self assert: v3 firstNorm equals: 2.
230+
231+
self assert: (v1 + v2) firstNorm equals: 3.
232+
self assert: (v2 + v3) firstNorm equals: 2
233+
]
234+
220235
{ #category : #tests }
221236
PMVectorTest >> testVectorGreater [
222237

src/Math-Vector/PMVector.class.st

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ PMVector >> cumsum [
181181

182182
]
183183

184+
{ #category : #operation }
185+
PMVector >> firstNorm [
186+
"Answer the first norm of the receiver."
187+
188+
^ self inject: 0 into: [ :accu :e | accu + e abs ]
189+
]
190+
184191
{ #category : #operation }
185192
PMVector >> hadamardProduct: aVector [
186193
"Answers the elementwise product of the receiver with aVector."
@@ -252,10 +259,12 @@ PMVector >> normalized [
252259
{ #category : #operation }
253260
PMVector >> productWithVector: aVector [
254261
"Answers the scalar product of aVector with the receiver."
262+
255263
| n |
256264
n := 0.
257-
^self inject: 0
258-
into: [ :sum :each | n := n + 1. (aVector at: n) * each + sum]
265+
^ self inject: 0 into: [ :sum :each |
266+
n := n + 1.
267+
(aVector at: n) * each + sum ]
259268
]
260269

261270
{ #category : #operation }

0 commit comments

Comments
 (0)