Object-oriented
>>> from vector import Vector
>>> Vector((1, 2, 3))
Vector(1, 2, 3, ...)
>>> Vector.randn(3)
Vector(-0.5613820142699765, -0.028308921297709365, 0.8270724508948077, ...)
>>> Vector(3)
Vector(0, 0, 0, 1, ...)
The immutable Vector
class wraps all the mentioned functions into a tidy package, making them easier to use by enabling interaction through operators.
Its coefficients are internally stored as a tuple in the coef
attribute and therefore zero-indexed.
Vector operations return the same type (type(v+w)==type(v)
) so the class can easily be extended (to e.g. a polynomial class).
veczero = ()
module-attribute
Zero vector.
Vector
An infinite-dimensional vector class.
Its coefficients are internally stored as a tuple in the coef
attribute.
Source code in vector\objectoriented.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|
ZERO
instance-attribute
Zero vector.
See also
__init__(coef)
Create a new vector with the given coefficients
or the i
-th basis vector if an integer i
is given.
Notes
- varargs (single argument=basis or multiple args=coefficients)? No, because then a single coefficient vector can't be distinguished from an index for a basis vector.
- Automatically trim on creation? Nah, other functions also don't do that.
Source code in vector\objectoriented.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
rand(n)
staticmethod
Create a random vector of n
uniform coefficients in [0, 1[
.
See also
Source code in vector\objectoriented.py
41 42 43 44 45 46 47 48 49 |
|
randn(n, normed=True, mu=0, sigma=1)
staticmethod
Create a random vector of n
normal distributed coefficients.
See also
Source code in vector\objectoriented.py
51 52 53 54 55 56 57 58 59 |
|
__len__()
Return the number of set coefficients.
Source code in vector\objectoriented.py
63 64 65 |
|
__getitem__(key)
Return the indexed coefficient or coefficients.
Not set coefficients default to 0.
Source code in vector\objectoriented.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
__iter__()
Return an iterator over the set coefficients.
Source code in vector\objectoriented.py
89 90 91 |
|
__eq__(other)
Return if of same type with same coefficients.
See also
Source code in vector\objectoriented.py
93 94 95 96 97 98 99 100 |
|
__lshift__(other)
Return a vector with coefficients shifted to lower indices.
See also
Source code in vector\objectoriented.py
103 104 105 106 107 108 109 110 |
|
__rshift__(other)
Return a vector with coefficients shifted to higher indices.
See also
Source code in vector\objectoriented.py
112 113 114 115 116 117 118 119 |
|
trim(tol=1e-09)
Remove all trailing near zero (abs<=tol) coefficients.
See also
Source code in vector\objectoriented.py
123 124 125 126 127 128 129 130 |
|
round(ndigits=None)
Round all coefficients to the given precision.
See also
Source code in vector\objectoriented.py
132 133 134 135 136 137 138 139 |
|
is_parallel(other)
Return if the other vector is parallel.
See also
Source code in vector\objectoriented.py
141 142 143 144 145 146 147 148 |
|
absq()
Return the sum of absolute squares of the coefficients.
See also
Source code in vector\objectoriented.py
152 153 154 155 156 157 158 159 |
|
__abs__()
Return the Euclidean/L2-norm.
Return the square root of vecabsq
.
See also
Source code in vector\objectoriented.py
161 162 163 164 165 166 167 168 169 170 |
|
__matmul__(other)
Return the real dot product of two vectors.
No argument is complex conjugated. All coefficients are used as is.
See also
Source code in vector\objectoriented.py
172 173 174 175 176 177 178 179 180 181 |
|
__pos__()
Return the unary positive.
See also
Source code in vector\objectoriented.py
186 187 188 189 190 191 192 193 |
|
__neg__()
Return the negative.
See also
Source code in vector\objectoriented.py
195 196 197 198 199 200 201 202 |
|
__add__(other)
Return the vector sum.
See also
Source code in vector\objectoriented.py
204 205 206 207 208 209 210 211 |
|
addc(c, i=0)
Return the sum with the i
-th basis vector times c
.
See also
Source code in vector\objectoriented.py
214 215 216 217 218 219 220 221 |
|
__sub__(other)
Return the vector difference.
See also
Source code in vector\objectoriented.py
223 224 225 226 227 228 229 230 |
|
__mul__(other)
Return the scalar product.
See also
Source code in vector\objectoriented.py
234 235 236 237 238 239 240 241 |
|
__truediv__(other)
Return the scalar true division.
See also
Source code in vector\objectoriented.py
244 245 246 247 248 249 250 251 |
|
__floordiv__(other)
Return the scalar floor division.
See also
Source code in vector\objectoriented.py
253 254 255 256 257 258 259 260 |
|
__mod__(other)
Return the elementwise mod with a scalar.
See also
Source code in vector\objectoriented.py
262 263 264 265 266 267 268 269 |
|
__divmod__(other)
Return the elementwise divmod with a scalar.
See also
Source code in vector\objectoriented.py
271 272 273 274 275 276 277 278 279 |
|
hadamard(other)
Return the elementwise product with another vector.
See also
Source code in vector\objectoriented.py
283 284 285 286 287 288 289 290 |
|
hadamardtruediv(other)
Return the elementwise true division with another vector.
See also
Source code in vector\objectoriented.py
292 293 294 295 296 297 298 299 |
|
hadamardfloordiv(other)
Return the elementwise floor division with another vector.
See also
Source code in vector\objectoriented.py
301 302 303 304 305 306 307 308 |
|
hadamardmod(other)
Return the elementwise mod with another vector.
See also
Source code in vector\objectoriented.py
310 311 312 313 314 315 316 317 |
|
hadamardmin(other)
Return the elementwise minimum with another vector.
See also
Source code in vector\objectoriented.py
319 320 321 322 323 324 325 326 |
|
hadamardmax(other)
Return the elementwise maximum with another vector.
See also
Source code in vector\objectoriented.py
328 329 330 331 332 333 334 335 |
|
vecbasis(i, c=1)
Return the i
-th basis vector times c
.
Returns a tuple with i
zeros followed by c
.
Source code in vector\functional.py
33 34 35 36 37 38 39 40 41 42 |
|
vecbasisgen()
Yield all basis vectors.
See also
- for single basis vector:
vecbasis
Source code in vector\functional.py
44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
vecrand(n)
Return a random vector of n
uniform float
coefficients in [0, 1[
.
Notes
Naming like in numpy.random
, because seems more concise
(not random
& gauss
as in the stdlib).
Source code in vector\functional.py
58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
vecrandn(n, normed=True, mu=0, sigma=1)
Return a random vector of n
normal distributed float
coefficients.
Notes
Naming like in numpy.random
, because seems more concise
(not random
& gauss
as in the stdlib).
Source code in vector\functional.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
veceq(v, w)
Return if two vectors are equal.
Source code in vector\functional.py
89 90 91 92 93 94 95 96 |
|
vectrim(v, tol=1e-09)
Remove all trailing near zero (abs(v_i)<=tol
) coefficients.
Notes
- Cutting of elements that are
abs(vi)<=tol
instead ofabs(vi)<tol
to allow cutting of elements that are exactly zero bytrim(v, 0)
instead oftrim(v, sys.float_info.min)
. tol=1e-9
like in PEP 485.
Source code in vector\functional.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
vecround(v, ndigits=None)
Round all coefficients to the given precision.
Source code in vector\functional.py
128 129 130 131 132 133 134 135 |
|
vecrshift(v, n, fill=0)
Pad n
many fill
s to the beginning of the vector.
Source code in vector\functional.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
veclshift(v, n)
Remove n
many coefficients at the beginning of the vector.
Source code in vector\functional.py
153 154 155 156 157 158 159 160 161 162 163 164 |
|
vecabsq(v)
Return the sum of absolute squares of the coefficients.
Notes
Reasons why it exists:
- Occurs in math.
- Most importantly: type independent because it doesn't use
sqrt
.
References
Source code in vector\functional.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
vecabs(v)
Return the Euclidean/L2-norm.
Returns the square root of vecabsq
.
Source code in vector\functional.py
189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
vecdot(v, w)
Return the inner product of two vectors without conjugation.
Source code in vector\functional.py
203 204 205 206 207 208 209 210 211 212 213 |
|
vecparallel(v, w)
Return if two vectors are parallel.
Source code in vector\functional.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
vecpos(v)
Return the vector with the unary positive operator applied.
Source code in vector\functional.py
235 236 237 238 239 240 241 242 |
|
vecneg(v)
Return the vector with the unary negative operator applied.
Source code in vector\functional.py
244 245 246 247 248 249 250 251 |
|
vecadd(*vs)
Return the sum of vectors.
Source code in vector\functional.py
253 254 255 256 257 258 259 260 |
|
vecaddc(v, c, i=0)
Return v
with c
added to the i
-th coefficient.
More efficient than vecadd(v, vecbasis(i, c))
.
Source code in vector\functional.py
262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
vecsub(v, w)
Return the difference of two vectors.
Source code in vector\functional.py
276 277 278 279 280 281 282 283 |
|
vecmul(a, v)
Return the product of a scalar and a vector.
Source code in vector\functional.py
285 286 287 288 289 290 291 292 |
|
vectruediv(v, a)
Return the true division of a vector and a scalar.
Notes
Why called truediv
instead of div
?
div
would be more appropriate for an absolute clean mathematical implementation, that doesn't care about the language used. But the package might be used for pure integers/integer arithmetic, so both,truediv
andfloordiv
operations have to be provided, and none should be privileged over the other by getting the universaldiv
name.truediv
/floordiv
is unambiguous, like Pythonoperator
s.
Source code in vector\functional.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
vecfloordiv(v, a)
Return the floor division of a vector and a scalar.
Source code in vector\functional.py
314 315 316 317 318 319 320 321 |
|
vecmod(v, a)
Return the elementwise mod of a vector and a scalar.
Source code in vector\functional.py
323 324 325 326 327 328 329 330 |
|
vecdivmod(v, a)
Return the elementwise divmod of a vector and a scalar.
Source code in vector\functional.py
332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
vechadamard(*vs)
Return the elementwise product of vectors.
Source code in vector\functional.py
348 349 350 351 352 353 354 355 |
|
vechadamardtruediv(v, w)
Return the elementwise true division of two vectors.
Source code in vector\functional.py
357 358 359 360 361 362 363 364 |
|
vechadamardfloordiv(v, w)
Return the elementwise floor division of two vectors.
Source code in vector\functional.py
366 367 368 369 370 371 372 373 |
|
vechadamardmod(v, w)
Return the elementwise mod of two vectors.
Source code in vector\functional.py
375 376 377 378 379 380 381 382 |
|
vechadamardmin(*vs)
Return the elementwise minimum of vectors.
Source code in vector\functional.py
384 385 386 387 388 389 390 391 |
|
vechadamardmax(*vs)
Return the elementwise maximum of vectors.
Source code in vector\functional.py
393 394 395 396 397 398 399 400 |
|