Object-oriented
Vector class.
>>> 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).
Vector
An infinite-dimensional vector class.
Its coefficients are internally stored as a tuple in the coef attribute.
Source code in vector\objectoriented.py
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
ZERO
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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
rand(n)
Create a random vector of n uniform coefficients in [0, 1[.
See also
Source code in vector\objectoriented.py
60 61 62 63 64 65 66 67 68 | |
randn(n, normed=True, mu=0, sigma=1)
Create a random vector of n normal distributed coefficients.
See also
Source code in vector\objectoriented.py
70 71 72 73 74 75 76 77 78 | |
__len__()
Return the number of set coefficients.
Source code in vector\objectoriented.py
82 83 84 | |
__getitem__(key)
Return the indexed coefficient or coefficients.
Not set coefficients default to 0.
Source code in vector\objectoriented.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
__iter__()
Return an iterator over the set coefficients.
Source code in vector\objectoriented.py
108 109 110 | |
__eq__(other)
Return if of same type with same coefficients.
See also
Source code in vector\objectoriented.py
112 113 114 115 116 117 118 119 | |
__lshift__(other)
Return a vector with coefficients shifted to lower indices.
See also
Source code in vector\objectoriented.py
122 123 124 125 126 127 128 129 | |
__rshift__(other)
Return a vector with coefficients shifted to higher indices.
See also
Source code in vector\objectoriented.py
131 132 133 134 135 136 137 138 | |
trim(tol=1e-09)
Remove all trailing near zero (abs<=tol) coefficients.
See also
Source code in vector\objectoriented.py
142 143 144 145 146 147 148 149 | |
round(ndigits=None)
Round all coefficients to the given precision.
See also
Source code in vector\objectoriented.py
151 152 153 154 155 156 157 158 | |
is_parallel(other)
Return if the other vector is parallel.
See also
Source code in vector\objectoriented.py
160 161 162 163 164 165 166 167 | |
absq()
Return the sum of absolute squares of the coefficients.
See also
Source code in vector\objectoriented.py
171 172 173 174 175 176 177 178 | |
__abs__()
Return the Euclidean/L2-norm.
Return the square root of vecabsq.
See also
Source code in vector\objectoriented.py
180 181 182 183 184 185 186 187 188 189 | |
__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
191 192 193 194 195 196 197 198 199 200 | |
__pos__()
Return the unary positive.
See also
Source code in vector\objectoriented.py
205 206 207 208 209 210 211 212 | |
__neg__()
Return the negative.
See also
Source code in vector\objectoriented.py
214 215 216 217 218 219 220 221 | |
__add__(other)
Return the vector sum.
See also
Source code in vector\objectoriented.py
223 224 225 226 227 228 229 230 | |
addc(c, i=0)
Return the sum with the i-th basis vector times c.
See also
Source code in vector\objectoriented.py
233 234 235 236 237 238 239 240 | |
__sub__(other)
Return the vector difference.
See also
Source code in vector\objectoriented.py
242 243 244 245 246 247 248 249 | |
__mul__(other)
Return the scalar product.
See also
Source code in vector\objectoriented.py
253 254 255 256 257 258 259 260 | |
__truediv__(other)
Return the scalar true division.
See also
Source code in vector\objectoriented.py
263 264 265 266 267 268 269 270 | |
__floordiv__(other)
Return the scalar floor division.
See also
Source code in vector\objectoriented.py
272 273 274 275 276 277 278 279 | |
__mod__(other)
Return the elementwise mod with a scalar.
See also
Source code in vector\objectoriented.py
281 282 283 284 285 286 287 288 | |
__divmod__(other)
Return the elementwise divmod with a scalar.
See also
Source code in vector\objectoriented.py
290 291 292 293 294 295 296 297 298 | |
hadamard(other)
Return the elementwise product with another vector.
See also
Source code in vector\objectoriented.py
302 303 304 305 306 307 308 309 | |
hadamardtruediv(other)
Return the elementwise true division with another vector.
See also
Source code in vector\objectoriented.py
311 312 313 314 315 316 317 318 | |
hadamardfloordiv(other)
Return the elementwise floor division with another vector.
See also
Source code in vector\objectoriented.py
320 321 322 323 324 325 326 327 | |
hadamardmod(other)
Return the elementwise mod with another vector.
See also
Source code in vector\objectoriented.py
329 330 331 332 333 334 335 336 | |
hadamardmin(other)
Return the elementwise minimum with another vector.
See also
Source code in vector\objectoriented.py
338 339 340 341 342 343 344 345 | |
hadamardmax(other)
Return the elementwise maximum with another vector.
See also
Source code in vector\objectoriented.py
347 348 349 350 351 352 353 354 | |