Skip to content

Commit

Permalink
Merge pull request #1 from PolyMathOrg/adding-first-norm
Browse files Browse the repository at this point in the history
Add first norm and test
  • Loading branch information
jordanmontt authored Apr 17, 2023
2 parents 5fb38e9 + f61d326 commit 4ff0c0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/Math-Vector-Tests/PMVectorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ PMVectorTest >> testVectorCumulativeSum [
self assert: (w at: 3) equals: 6
]

{ #category : #tests }
PMVectorTest >> testVectorFirstNorm [

| v1 v2 v3 |
v1 := #( 1 0 ) asPMVector.
self assert: v1 firstNorm equals: 1.
v2 := #( 1 1 ) asPMVector.
self assert: v2 firstNorm equals: 2.
v3 := #( -1 1 ) asPMVector.
self assert: v3 firstNorm equals: 2.

self assert: (v1 + v2) firstNorm equals: 3.
self assert: (v2 + v3) firstNorm equals: 2
]

{ #category : #tests }
PMVectorTest >> testVectorGreater [

Expand Down
13 changes: 11 additions & 2 deletions src/Math-Vector/PMVector.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ PMVector >> cumsum [

]

{ #category : #operation }
PMVector >> firstNorm [
"Answer the first norm of the receiver."

^ self inject: 0 into: [ :accu :e | accu + e abs ]
]

{ #category : #operation }
PMVector >> hadamardProduct: aVector [
"Answers the elementwise product of the receiver with aVector."
Expand Down Expand Up @@ -252,10 +259,12 @@ PMVector >> normalized [
{ #category : #operation }
PMVector >> productWithVector: aVector [
"Answers the scalar product of aVector with the receiver."

| n |
n := 0.
^self inject: 0
into: [ :sum :each | n := n + 1. (aVector at: n) * each + sum]
^ self inject: 0 into: [ :sum :each |
n := n + 1.
(aVector at: n) * each + sum ]
]

{ #category : #operation }
Expand Down

0 comments on commit 4ff0c0f

Please sign in to comment.