• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PolyMathOrg / PolyMath / 3621631292

pending completion
3621631292

push

github

GitHub
Merge pull request #292 from jecisc/cut-dependency-from-Core-to-Complex

3 of 3 new or added lines in 1 file covered. (100.0%)

11931 of 16386 relevant lines covered (72.81%)

0.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.67
/src/Math-Core/PMVector.class.st
1
"
2
A vector is an object in a multidimensional space. It is represented by its components measured on a reference system.
3

4
Here is a typical use of myself
5

6
[[[ 
7
| u v w a b c |
8
u := #(1 2 3) asPMVector.
9
v := #(3 4 5) asPMVector.
10
a := PMMatrix rows: #( ( 1 0 1 ) (-1 -2 3)).
11
b := PMMatrix rows: #( ( 1 2 3 ) (-2 1 7) (5 6 7)).
12
w := 4 * u + (3 * v).
13
c := a * b.
14
v := a * u.
15
w := c transpose * v.
16
w := v * c.
17
]]]
18
"
19
Class {
20
        #name : #PMVector,
21
        #superclass : #Array,
22
        #type : #variable,
23
        #category : #'Math-Core'
24
}
25

26
{ #category : #'instance creation' }
27
PMVector class >> new: size random: maxRandomNumber [ 
1✔
28
        "Answer an instance of me, with number of elements equal to size, each 
1✔
29
        a randomNumber lower than maxRandomNumber ."
1✔
30
        
1✔
31
        | random |
1✔
32
        random := Random new.
1✔
33

1✔
34
        ^ self newFrom: ((1 to: size ) collect: [ :each |
1✔
35
                random nextBetween: 0 and: maxRandomNumber ]).
1✔
36
]
1✔
37

38
{ #category : #private }
39
PMVector class >> ones: anInteger [
1✔
40
        "Creates a vector of ones."
1✔
41
        |a|
1✔
42
        a := PMVector new: anInteger.
1✔
43
        1 to: a size do: [ :n | a at: n put: 1].
1✔
44
        ^a
1✔
45
]
1✔
46

47
{ #category : #private }
48
PMVector class >> randomSize: anInteger maxNumber: aMaxNumber [
×
49
        "Creates a vector of rand numbers from 0 to aMaxNumber."
×
50
        | random vector |
×
51
        random := Random new.
×
52
        
×
53
        vector := PMVector new: anInteger.
×
54
        1 to: vector size do: [ :i |
×
55
                vector at: i put: (random nextBetween: 0 and: aMaxNumber)].
×
56
        
×
57
        ^ vector
×
58
]
×
59

60
{ #category : #private }
61
PMVector class >> zeros: anInteger [
1✔
62
        "Creates a vector of zeros."
1✔
63
        |a|
1✔
64
        a := PMVector new: anInteger.
1✔
65
        1 to: a size do: [ :n | a at: n put: 0].
1✔
66
        ^a
1✔
67
]
1✔
68

69
{ #category : #operation }
70
PMVector >> * aNumberaVectoraMatrix [
1✔
71
        ^ aNumberaVectoraMatrix productWithVector: self
1✔
72
]
1✔
73

74
{ #category : #operation }
75
PMVector >> + aVectorOrNumber [
1✔
76
        "Answers the sum of the receiver with a vector or number."
1✔
77
        ^ aVectorOrNumber addWithVector: self
1✔
78
]
1✔
79

80
{ #category : #operation }
81
PMVector >> - aVectorOrNumber [
1✔
82
        "Answers the difference of the receiver with a vector or number."
1✔
83
        ^ -1*aVectorOrNumber addWithVector: self
1✔
84
]
1✔
85

86
{ #category : #operation }
87
PMVector >> < aNumber [
1✔
88
        "Apply < operator to every element of a vector and returns a new vector"
1✔
89
        ^ ((1 to: self size) collect: [ :n | (self at: n) < aNumber]) asPMVector.
1✔
90
]
1✔
91

92
{ #category : #operation }
93
PMVector >> > aNumber [
94
        "Apply > function to every element of a vector and return a new vector"
95
        ^ ((1 to: self size) collect: [ :n | (self at: n) > aNumber]) asPMVector.
96
]
97

98
{ #category : #transformation }
99
PMVector >> accumulate: aVectorOrAnArray [
1✔
100
        "Modify the receiver adding the contents of the argument to the receiver."
1✔
101
        1 to: self size do: [ :n | self at: n put: ((self at: n) + (aVectorOrAnArray at: n))].
1✔
102
]
1✔
103

104
{ #category : #transformation }
105
PMVector >> accumulateNegated: aVectorOrAnArray [
1✔
106
        "Modify the receiver adding the negated contents of the argument to the receiver."
1✔
107
        1 to: self size do: [ :n | self at: n put: ((self at: n) - (aVectorOrAnArray at: n))].
1✔
108
]
1✔
109

110
{ #category : #'as yet unclassified' }
111
PMVector >> argMax [
1✔
112
        | a |
1✔
113
        a := self asArray.
1✔
114
        ^ a indexOf: a max
1✔
115
]
1✔
116

117
{ #category : #converting }
118
PMVector >> asArray [
1✔
119
        
1✔
120
        | array i|
1✔
121
        array := Array new: self size.
1✔
122
        i := 0.
1✔
123
        self do: [:item | array basicAt: (i:=i+1) put: item].
1✔
124
        ^ array
1✔
125
]
1✔
126

127
{ #category : #creation }
128
PMVector >> asPMVector [
1✔
129
        "Answer self since the receiver is a vector."
1✔
130
        ^ self
1✔
131
]
1✔
132

133
{ #category : #'as yet unclassified' }
134
PMVector >> checkDimensionalCompatibility: dimensionArray [
1✔
135
        |prod|
1✔
136
        prod := 1.
1✔
137
        
1✔
138
        dimensionArray do: [ :each | prod := prod * each ].
1✔
139
        
1✔
140
        self assert: (self size = prod) description: 'Imcompatible combination of Dimensions provided'.
1✔
141
        
1✔
142
        ^true 
1✔
143
]
1✔
144

145
{ #category : #comparing }
146
PMVector >> closeTo: aPMVector [
1✔
147
        "Compare two vectors using the default precision from Float >> #closeTo:."
1✔
148

1✔
149
        ^ self closeTo: aPMVector precision: 0.0001.
1✔
150
]
1✔
151

152
{ #category : #comparing }
153
PMVector >> closeTo: aPMVector precision: aPrecision [
1✔
154
        "Tests whether the total summed difference between self and aPMVector is within aPrecision."
1✔
155

1✔
156
        ^ (self - aPMVector) abs sum < aPrecision
1✔
157
]
1✔
158

159
{ #category : #operation }
160
PMVector >> cos [
1✔
161
        "Apply cos function to every element of a vector"
1✔
162
        1 to: self size do: [ :n | self at: n put: (self at: n) cos].
1✔
163
]
1✔
164

165
{ #category : #operation }
166
PMVector >> cosh [
×
167
        "Apply cosh function to every element of a vector"
×
168
        1 to: self size do: [ :n | self at: n put: (self at: n) cosh].
×
169
]
×
170

171
{ #category : #transformation }
172
PMVector >> cumsum [
1✔
173
        "Cumulative sum
1✔
174
        #(1 2 3 4 5) cumsum = #(1 3 6 10 15)
1✔
175
        "
1✔
176
   | sum |
1✔
177
        sum := 0.
1✔
178
        ^ self collect: [ :v | sum := sum + v. sum ]
1✔
179
 
1✔
180
]
1✔
181

182
{ #category : #operation }
183
PMVector >> hadamardProduct: aVector [
1✔
184
        "Answers the elementwise product of the receiver with aVector."
1✔
185

1✔
186
        | answer n |
1✔
187
        answer := self class new: self size.
1✔
188
        n := 0.
1✔
189
        self
1✔
190
                with: aVector
1✔
191
                do: [ :a :b | 
1✔
192
                        n := n + 1.
1✔
193
                        answer at: n put: a * b ].
1✔
194
        ^ answer
1✔
195
]
1✔
196

197
{ #category : #'as yet unclassified' }
198
PMVector >> householder [
1✔
199
        "returns a collection of the skalar beta and the housholder vector"
1✔
200
        |s v b u x |
1✔
201
        s :=self allButFirst.
1✔
202
        s := s *s.
1✔
203
        v := self copy.
1✔
204
        v at: 1 put: 1.  
1✔
205
        s = 0 
1✔
206
                ifTrue: [ b :=0 ] 
1✔
207
                ifFalse: [
1✔
208
                        u :=((x:=self at:1)squared + s)sqrt .
1✔
209
                        v 
1✔
210
                                at: 1 
1✔
211
                                put: ((x <=0) ifTrue: [x -u] ifFalse:  [0 - s / (x + u)]).
1✔
212
                        b :=(v at: 1) squared * 2 / (s + (v at: 1) squared).
1✔
213
                        v := v / (v at: 1) ].
1✔
214
        ^Array with: b with: v
1✔
215
]
1✔
216

217
{ #category : #testing }
218
PMVector >> isZero [
1✔
219
 ^ self allSatisfy: [ :element | element = 0 ]
1✔
220
]
1✔
221

222
{ #category : #operation }
223
PMVector >> log [
×
224
        "Apply log function to every element of a vector"
×
225
        1 to: self size do: [ :n | self at: n put: (self at: n) log].
×
226
]
×
227

228
{ #category : #transformation }
229
PMVector >> negate [
1✔
230
        "Inverse the sign of all components of the receiver."
1✔
231
        1 to: self size do: [ :n | self at: n put: (self at: n) negated].
1✔
232
]
1✔
233

234
{ #category : #operation }
235
PMVector >> negated [
1✔
236
        ^ self * -1
1✔
237
]
1✔
238

239
{ #category : #operation }
240
PMVector >> norm [
1✔
241
        "Answer the norm of the receiver."
1✔
242
        ^(self * self) sqrt
1✔
243
]
1✔
244

245
{ #category : #creation }
246
PMVector >> normalized [
1✔
247
        ^ (1 / self norm) * self
1✔
248
]
1✔
249

250
{ #category : #operation }
251
PMVector >> productWithVector: aVector [
1✔
252
        "Answers the scalar product of aVector with the receiver."
1✔
253
        | n |
1✔
254
        n := 0.
1✔
255
        ^self inject: 0
1✔
256
                        into: [ :sum :each | n := n + 1. (aVector at: n) * each + sum]
1✔
257
]
1✔
258

259
{ #category : #operation }
260
PMVector >> scalarProduct: aVector [
1✔
261
        
1✔
262
        | product n |
1✔
263
        n := 0.
1✔
264
        product := self collect: [ :each | n := n + 1. (aVector at: n) * each].
1✔
265
        n := product size.
1✔
266
        [ n > 1]
1✔
267
                whileTrue:[ | i j|
1✔
268
                                        i := 1.
1✔
269
                                        j := n.
1✔
270
                                        [ i < j]
1✔
271
                                                whileTrue: [ product at: i put: ( product at: i) + ( product at: j).
1✔
272
                                                                         j := j - 1.
1✔
273
                                                                         i := i + 1.
1✔
274
                                                                   ].
1✔
275
                                        n := i min: j.
1✔
276
                                  ].
1✔
277
        ^product at: 1
1✔
278
]
1✔
279

280
{ #category : #transformation }
281
PMVector >> scaleBy: aNumber [
1✔
282
        "Modify the receiver elements by a multiplicating factor."
1✔
283
        1 to: self size do: [ :n | self at: n put: ((self at: n) * aNumber)].
1✔
284
]
1✔
285

286
{ #category : #operation }
287
PMVector >> sin [
×
288
        "Apply sin function to every element of a vector"
×
289

×
290
        1 to: self size do: [ :n | self at: n put: (self at: n) sin ]
×
291
]
×
292

293
{ #category : #operation }
294
PMVector >> sinh [
×
295
        "Apply sinh function to every element of a vector"
×
296

×
297
        1 to: self size do: [ :n | self at: n put: (self at: n) sinh ]
×
298
]
×
299

300
{ #category : #operation }
301
PMVector >> sqrt [
1✔
302
        "Apply sqrt function to every element of a vector"
1✔
303

1✔
304
        1 to: self size do: [ :n | self at: n put: (self at: n) sqrt ]
1✔
305
]
1✔
306

307
{ #category : #arithmetic }
308
PMVector >> take: anInteger [
1✔
309

1✔
310
        " Answer a <Collection> with the receiver's binomial coefficient at each element for anInteger "
1✔
311

1✔
312
        ^ self collect: [ :each | each numberOfCombinationsTaken: anInteger ]
1✔
313
]
1✔
314

315
{ #category : #operation }
316
PMVector >> tan [
×
317
        "Apply tan function to every element of a vector"
×
318

×
319
        1 to: self size do: [ :n | self at: n put: (self at: n) tan ]
×
320
]
×
321

322
{ #category : #operation }
323
PMVector >> tanh [
×
324
        "Apply tanh function to every element of a vector"
×
325

×
326
        ^ self collect: [ :each | each tanh ]
×
327
]
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc