Skip to content

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
class Vector:
    """An infinite-dimensional vector class.

    Its coefficients are internally stored as a tuple in the `coef` attribute.
    """

    ZERO: 'Vector'
    """Zero vector.

    See also
    --------
    [`veczero`][vector.functional.veczero]
    """

    #creation
    def __init__(self, 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.
        """
        if isinstance(coef, int):
            self.coef = vecbasis(coef)
        else:
            self.coef = tuple(coef)

    @staticmethod
    def rand(n):
        """Create a random vector of `n` uniform coefficients in `[0, 1[`.

        See also
        --------
        [`vecrand`][vector.functional.vecrand]
        """
        return Vector(vecrand(n))

    @staticmethod
    def randn(n, normed=True, mu=0, sigma=1):
        """Create a random vector of `n` normal distributed coefficients.

        See also
        --------
        [`vecrandn`][vector.functional.vecrandn]
        """
        return Vector(vecrandn(n, normed, mu, sigma))


    #sequence
    def __len__(self):
        """Return the number of set coefficients."""
        return len(self.coef)

    def __getitem__(self, key):
        """Return the indexed coefficient or coefficients.

        Not set coefficients default to 0.
        """
        #getter has if and try-else
        #might access coef directly in the following methods
        #for better performance
        #on the other hand most functions access the iterator
        #that directly goes for the tuple
        if isinstance(key, slice):
            #enable stop>=len with zero padding
            if key.stop is not None and key.stop >= 0:
                l = key.stop
            else:
                l = len(self)
            return Vector(self[i] for i in range(*key.indices(l)))
        try:
            return self.coef[key]
        except IndexError:
            return 0

    def __iter__(self):
        """Return an iterator over the set coefficients."""
        return iter(self.coef)

    def __eq__(self, other):
        """Return if of same type with same coefficients.

        See also
        --------
        [`veceq`][vector.functional.veceq]
        """
        return isinstance(other, type(self)) and veceq(self, other)


    def __lshift__(self, other):
        """Return a vector with coefficients shifted to lower indices.

        See also
        --------
        [`veclshift`][vector.functional.veclshift]
        """
        return type(self)(self[other:])

    def __rshift__(self, other):
        """Return a vector with coefficients shifted to higher indices.

        See also
        --------
        [`vecrshift`][vector.functional.vecrshift]
        """
        return type(self)((0,)*other + self.coef)


    #utility
    def trim(self, tol=1e-9):
        """Remove all trailing near zero (abs<=tol) coefficients.

        See also
        --------
        [`vectrim`][vector.functional.vectrim]
        """
        return type(self)(vectrim(self, tol))

    def round(self, ndigits=None):
        """Round all coefficients to the given precision.

        See also
        --------
        [`vecround`][vector.functional.vecround]
        """
        return type(self)(vecround(self, ndigits))

    def is_parallel(self, other):
        """Return if the other vector is parallel.

        See also
        --------
        [`vecparallel`][vector.functional.vecparallel]
        """
        return vecparallel(self, other)


    #Hilbert space
    def absq(self):
        """Return the sum of absolute squares of the coefficients.

        See also
        --------
        [`vecabsq`][vector.functional.vecabsq]
        """
        return vecabsq(self)

    def __abs__(self):
        """Return the Euclidean/L2-norm.

        Return the square root of `vecabsq`.

        See also
        --------
        [`vecabs`][vector.functional.vecabs]
        """
        return self.absq()**0.5

    def __matmul__(self, other):
        """Return the real dot product of two vectors.

        No argument is complex conjugated. All coefficients are used as is.

        See also
        --------
        [`vecdot`][vector.functional.vecdot]
        """
        return vecdot(self, other)


    #vector space operations like they would be correct on paper:
    #+v, -v, v+w, v-w, av, va, v/a, v//a
    def __pos__(self):
        """Return the unary positive.

        See also
        --------
        [`vecpos`][vector.functional.vecpos]
        """
        return type(self)(vecpos(self))

    def __neg__(self):
        """Return the negative.

        See also
        --------
        [`vecneg`][vector.functional.vecneg]
        """
        return type(self)(vecneg(self))

    def __add__(self, other):
        """Return the vector sum.

        See also
        --------
        [`vecadd`][vector.functional.vecadd]
        """
        return type(self)(vecadd(self, other))
    __radd__ = __add__

    def addc(self, c, i=0):
        """Return the sum with the `i`-th basis vector times `c`.

        See also
        --------
        [`vecaddc`][vector.functional.vecaddc]
        """
        return type(self)(vecaddc(self, c, i))

    def __sub__(self, other):
        """Return the vector difference.

        See also
        --------
        [`vecsub`][vector.functional.vecsub]
        """
        return type(self)(vecsub(self, other))
    def __rsub__(self, other):
        return type(self)(vecsub(other, self))

    def __mul__(self, other):
        """Return the scalar product.

        See also
        --------
        [`vecmul`][vector.functional.vecmul]
        """
        return type(self)(vecmul(other, self))
    __rmul__ = __mul__

    def __truediv__(self, other):
        """Return the scalar true division.

        See also
        --------
        [`vectruediv`][vector.functional.vectruediv]
        """
        return type(self)(vectruediv(self, other))

    def __floordiv__(self, other):
        """Return the scalar floor division.

        See also
        --------
        [`vecfloordiv`][vector.functional.vecfloordiv]
        """
        return type(self)(vecfloordiv(self, other))

    def __mod__(self, other):
        """Return the elementwise mod with a scalar.

        See also
        --------
        [`vecmod`][vector.functional.vecmod]
        """
        return type(self)(vecmod(self, other))

    def __divmod__(self, other):
        """Return the elementwise divmod with a scalar.

        See also
        --------
        [`vecdivmod`][vector.functional.vecdivmod]
        """
        q, r = vecdivmod(self, other)
        return type(self)(q), type(self)(r)


    #elementwise
    def hadamard(self, other):
        """Return the elementwise product with another vector.

        See also
        --------
        [`vechadamard`][vector.functional.vechadamard]
        """
        return type(self)(vechadamard(self, other))

    def hadamardtruediv(self, other):
        """Return the elementwise true division with another vector.

        See also
        --------
        [`vechadamardtruediv`][vector.functional.vechadamardtruediv]
        """
        return type(self)(vechadamardtruediv(self, other))

    def hadamardfloordiv(self, other):
        """Return the elementwise floor division with another vector.

        See also
        --------
        [`vechadamardfloordiv`][vector.functional.vechadamardfloordiv]
        """
        return type(self)(vechadamardfloordiv(self, other))

    def hadamardmod(self, other):
        """Return the elementwise mod with another vector.

        See also
        --------
        [`vechadamardmod`][vector.functional.vechadamardmod]
        """
        return type(self)(vechadamardmod(self, other))

    def hadamardmin(self, other):
        """Return the elementwise minimum with another vector.

        See also
        --------
        [`vechadamardmin`][vector.functional.vechadamardmin]
        """
        return type(self)(vechadamardmin(self, other))

    def hadamardmax(self, other):
        """Return the elementwise maximum with another vector.

        See also
        --------
        [`vechadamardmax`][vector.functional.vechadamardmax]
        """
        return type(self)(vechadamardmax(self, other))


    #python stuff
    def __format__(self, format_spec):
        return 'Vector(' + ', '.join(format(c, format_spec) for c in self.coef) + ')'

    def __str__(self):
        return 'Vector(' + ', '.join(map(str, self.coef)) + ')'

ZERO

Zero vector.

See also

veczero

__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
def __init__(self, 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.
    """
    if isinstance(coef, int):
        self.coef = vecbasis(coef)
    else:
        self.coef = tuple(coef)

rand(n)

Create a random vector of n uniform coefficients in [0, 1[.

See also

vecrand

Source code in vector\objectoriented.py
60
61
62
63
64
65
66
67
68
@staticmethod
def rand(n):
    """Create a random vector of `n` uniform coefficients in `[0, 1[`.

    See also
    --------
    [`vecrand`][vector.functional.vecrand]
    """
    return Vector(vecrand(n))

randn(n, normed=True, mu=0, sigma=1)

Create a random vector of n normal distributed coefficients.

See also

vecrandn

Source code in vector\objectoriented.py
70
71
72
73
74
75
76
77
78
@staticmethod
def randn(n, normed=True, mu=0, sigma=1):
    """Create a random vector of `n` normal distributed coefficients.

    See also
    --------
    [`vecrandn`][vector.functional.vecrandn]
    """
    return Vector(vecrandn(n, normed, mu, sigma))

__len__()

Return the number of set coefficients.

Source code in vector\objectoriented.py
82
83
84
def __len__(self):
    """Return the number of set coefficients."""
    return len(self.coef)

__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
def __getitem__(self, key):
    """Return the indexed coefficient or coefficients.

    Not set coefficients default to 0.
    """
    #getter has if and try-else
    #might access coef directly in the following methods
    #for better performance
    #on the other hand most functions access the iterator
    #that directly goes for the tuple
    if isinstance(key, slice):
        #enable stop>=len with zero padding
        if key.stop is not None and key.stop >= 0:
            l = key.stop
        else:
            l = len(self)
        return Vector(self[i] for i in range(*key.indices(l)))
    try:
        return self.coef[key]
    except IndexError:
        return 0

__iter__()

Return an iterator over the set coefficients.

Source code in vector\objectoriented.py
108
109
110
def __iter__(self):
    """Return an iterator over the set coefficients."""
    return iter(self.coef)

__eq__(other)

Return if of same type with same coefficients.

See also

veceq

Source code in vector\objectoriented.py
112
113
114
115
116
117
118
119
def __eq__(self, other):
    """Return if of same type with same coefficients.

    See also
    --------
    [`veceq`][vector.functional.veceq]
    """
    return isinstance(other, type(self)) and veceq(self, other)

__lshift__(other)

Return a vector with coefficients shifted to lower indices.

See also

veclshift

Source code in vector\objectoriented.py
122
123
124
125
126
127
128
129
def __lshift__(self, other):
    """Return a vector with coefficients shifted to lower indices.

    See also
    --------
    [`veclshift`][vector.functional.veclshift]
    """
    return type(self)(self[other:])

__rshift__(other)

Return a vector with coefficients shifted to higher indices.

See also

vecrshift

Source code in vector\objectoriented.py
131
132
133
134
135
136
137
138
def __rshift__(self, other):
    """Return a vector with coefficients shifted to higher indices.

    See also
    --------
    [`vecrshift`][vector.functional.vecrshift]
    """
    return type(self)((0,)*other + self.coef)

trim(tol=1e-09)

Remove all trailing near zero (abs<=tol) coefficients.

See also

vectrim

Source code in vector\objectoriented.py
142
143
144
145
146
147
148
149
def trim(self, tol=1e-9):
    """Remove all trailing near zero (abs<=tol) coefficients.

    See also
    --------
    [`vectrim`][vector.functional.vectrim]
    """
    return type(self)(vectrim(self, tol))

round(ndigits=None)

Round all coefficients to the given precision.

See also

vecround

Source code in vector\objectoriented.py
151
152
153
154
155
156
157
158
def round(self, ndigits=None):
    """Round all coefficients to the given precision.

    See also
    --------
    [`vecround`][vector.functional.vecround]
    """
    return type(self)(vecround(self, ndigits))

is_parallel(other)

Return if the other vector is parallel.

See also

vecparallel

Source code in vector\objectoriented.py
160
161
162
163
164
165
166
167
def is_parallel(self, other):
    """Return if the other vector is parallel.

    See also
    --------
    [`vecparallel`][vector.functional.vecparallel]
    """
    return vecparallel(self, other)

absq()

Return the sum of absolute squares of the coefficients.

See also

vecabsq

Source code in vector\objectoriented.py
171
172
173
174
175
176
177
178
def absq(self):
    """Return the sum of absolute squares of the coefficients.

    See also
    --------
    [`vecabsq`][vector.functional.vecabsq]
    """
    return vecabsq(self)

__abs__()

Return the Euclidean/L2-norm.

Return the square root of vecabsq.

See also

vecabs

Source code in vector\objectoriented.py
180
181
182
183
184
185
186
187
188
189
def __abs__(self):
    """Return the Euclidean/L2-norm.

    Return the square root of `vecabsq`.

    See also
    --------
    [`vecabs`][vector.functional.vecabs]
    """
    return self.absq()**0.5

__matmul__(other)

Return the real dot product of two vectors.

No argument is complex conjugated. All coefficients are used as is.

See also

vecdot

Source code in vector\objectoriented.py
191
192
193
194
195
196
197
198
199
200
def __matmul__(self, other):
    """Return the real dot product of two vectors.

    No argument is complex conjugated. All coefficients are used as is.

    See also
    --------
    [`vecdot`][vector.functional.vecdot]
    """
    return vecdot(self, other)

__pos__()

Return the unary positive.

See also

vecpos

Source code in vector\objectoriented.py
205
206
207
208
209
210
211
212
def __pos__(self):
    """Return the unary positive.

    See also
    --------
    [`vecpos`][vector.functional.vecpos]
    """
    return type(self)(vecpos(self))

__neg__()

Return the negative.

See also

vecneg

Source code in vector\objectoriented.py
214
215
216
217
218
219
220
221
def __neg__(self):
    """Return the negative.

    See also
    --------
    [`vecneg`][vector.functional.vecneg]
    """
    return type(self)(vecneg(self))

__add__(other)

Return the vector sum.

See also

vecadd

Source code in vector\objectoriented.py
223
224
225
226
227
228
229
230
def __add__(self, other):
    """Return the vector sum.

    See also
    --------
    [`vecadd`][vector.functional.vecadd]
    """
    return type(self)(vecadd(self, other))

addc(c, i=0)

Return the sum with the i-th basis vector times c.

See also

vecaddc

Source code in vector\objectoriented.py
233
234
235
236
237
238
239
240
def addc(self, c, i=0):
    """Return the sum with the `i`-th basis vector times `c`.

    See also
    --------
    [`vecaddc`][vector.functional.vecaddc]
    """
    return type(self)(vecaddc(self, c, i))

__sub__(other)

Return the vector difference.

See also

vecsub

Source code in vector\objectoriented.py
242
243
244
245
246
247
248
249
def __sub__(self, other):
    """Return the vector difference.

    See also
    --------
    [`vecsub`][vector.functional.vecsub]
    """
    return type(self)(vecsub(self, other))

__mul__(other)

Return the scalar product.

See also

vecmul

Source code in vector\objectoriented.py
253
254
255
256
257
258
259
260
def __mul__(self, other):
    """Return the scalar product.

    See also
    --------
    [`vecmul`][vector.functional.vecmul]
    """
    return type(self)(vecmul(other, self))

__truediv__(other)

Return the scalar true division.

See also

vectruediv

Source code in vector\objectoriented.py
263
264
265
266
267
268
269
270
def __truediv__(self, other):
    """Return the scalar true division.

    See also
    --------
    [`vectruediv`][vector.functional.vectruediv]
    """
    return type(self)(vectruediv(self, other))

__floordiv__(other)

Return the scalar floor division.

See also

vecfloordiv

Source code in vector\objectoriented.py
272
273
274
275
276
277
278
279
def __floordiv__(self, other):
    """Return the scalar floor division.

    See also
    --------
    [`vecfloordiv`][vector.functional.vecfloordiv]
    """
    return type(self)(vecfloordiv(self, other))

__mod__(other)

Return the elementwise mod with a scalar.

See also

vecmod

Source code in vector\objectoriented.py
281
282
283
284
285
286
287
288
def __mod__(self, other):
    """Return the elementwise mod with a scalar.

    See also
    --------
    [`vecmod`][vector.functional.vecmod]
    """
    return type(self)(vecmod(self, other))

__divmod__(other)

Return the elementwise divmod with a scalar.

See also

vecdivmod

Source code in vector\objectoriented.py
290
291
292
293
294
295
296
297
298
def __divmod__(self, other):
    """Return the elementwise divmod with a scalar.

    See also
    --------
    [`vecdivmod`][vector.functional.vecdivmod]
    """
    q, r = vecdivmod(self, other)
    return type(self)(q), type(self)(r)

hadamard(other)

Return the elementwise product with another vector.

See also

vechadamard

Source code in vector\objectoriented.py
302
303
304
305
306
307
308
309
def hadamard(self, other):
    """Return the elementwise product with another vector.

    See also
    --------
    [`vechadamard`][vector.functional.vechadamard]
    """
    return type(self)(vechadamard(self, other))

hadamardtruediv(other)

Return the elementwise true division with another vector.

See also

vechadamardtruediv

Source code in vector\objectoriented.py
311
312
313
314
315
316
317
318
def hadamardtruediv(self, other):
    """Return the elementwise true division with another vector.

    See also
    --------
    [`vechadamardtruediv`][vector.functional.vechadamardtruediv]
    """
    return type(self)(vechadamardtruediv(self, other))

hadamardfloordiv(other)

Return the elementwise floor division with another vector.

See also

vechadamardfloordiv

Source code in vector\objectoriented.py
320
321
322
323
324
325
326
327
def hadamardfloordiv(self, other):
    """Return the elementwise floor division with another vector.

    See also
    --------
    [`vechadamardfloordiv`][vector.functional.vechadamardfloordiv]
    """
    return type(self)(vechadamardfloordiv(self, other))

hadamardmod(other)

Return the elementwise mod with another vector.

See also

vechadamardmod

Source code in vector\objectoriented.py
329
330
331
332
333
334
335
336
def hadamardmod(self, other):
    """Return the elementwise mod with another vector.

    See also
    --------
    [`vechadamardmod`][vector.functional.vechadamardmod]
    """
    return type(self)(vechadamardmod(self, other))

hadamardmin(other)

Return the elementwise minimum with another vector.

See also

vechadamardmin

Source code in vector\objectoriented.py
338
339
340
341
342
343
344
345
def hadamardmin(self, other):
    """Return the elementwise minimum with another vector.

    See also
    --------
    [`vechadamardmin`][vector.functional.vechadamardmin]
    """
    return type(self)(vechadamardmin(self, other))

hadamardmax(other)

Return the elementwise maximum with another vector.

See also

vechadamardmax

Source code in vector\objectoriented.py
347
348
349
350
351
352
353
354
def hadamardmax(self, other):
    """Return the elementwise maximum with another vector.

    See also
    --------
    [`vechadamardmax`][vector.functional.vechadamardmax]
    """
    return type(self)(vechadamardmax(self, other))