Skip to main content

A numerical methods tool for python, in python.

Project description

Vectorgebra

A numerical methods tool for python, in python.

There are 8 main subclasses; Variable, Vector, Matrix, Tensor, Graph, Complex, Infinity, Undefined. And also there are functions, constants and exception classes. Each section is explained below.

Project details

License: MIT

pip install vectorgebra

https://pypi.org/project/vectorgebra/

Github

A C++ remake of this library is currently being developed at here.

Tutorials for this library can be found at here.

What can be done with Vectorgebra?

Here is a cool little project I created with Vectorgebra. I want this library to be applicable to more and more bigger projects as scale. And this was a good test to it. Creating and training an ML model requires both floating point precision and good handling of high dimensional tensors.

Update notes on 3.3.0

This very update, may be the biggest and most important update of Vectorgebra, ever. This update introduces autograd functionality for Vectorgebra, and future MLgebra.

The new Variable class, is a basic numerical variable type that also builds a computational graph when put through mathematical operations. This graph, then can be utilized to calculate the gradient of the whole operation using the new "grad()" function.

"grad()" function, is a highly performant autograd implementation purely in Python, as the motto of this whole library. When measured with dot product operation, Its speed is comparable (equal or at max ~10 times slower) to Tensorflow up to graphs of deepness of 10000. At 1 million parameters, it is measured to be only ~300 times slower than Tensorflow, which is very surprising for pure Python.

More performance updates will come for autograd. This is probably not the maximum speed that we can squeeze out from CPython.

MLgebra will probably completely switch to this function for its backpropagation calculations in the very near future.

A new simple class, BinaryNode, is added to the library as well. This class is used to build separate and more performant computational graphs for gradient calculation. It is more lightweight compared to Variable, and is designed for only internal usage. This may be removed in later versions, do not rely on it.

".toVariable()" methods are added to both Vector and Matrix class to simplify the usage of Variable objects in linear algebra related tasks.

Vectorgebra.Variable

This is a wrapper class for all types of "variables", to generate computational graphs. It can be initialized with simply giving it a value to hold. After that, any Variable object can be simply used as they were the "value" given to them.

Every mathematical operation (except self assignments), logical operation and comparison operation are numerically passed to the wrapped "variable". If necessary, the operation is recorded on the computational graph.

Vectorgebra.Variable.disconnect()

Disconnects the node-self from the preceding parts of the graph. Sets backwards to None. Forward connection of the node remains. This is not a complete disconnection.

Always rerun the calculation of the graph, if you are going to calculate gradient after calling this function.

Vectorgebra.Variable.set(value)

Reset the value contained in the Variable object, if and only if the object is a leaf on the computational graph, aka backwards is None.

Vectorgebra.Variable.calculate()

Recalculate the value for the self-node, by doing a complete recalculation of the partial (or complete, given the node) computational graph from the ground up. Calculations are done internally for each node. No connection reassignments are done in the graph. Only the "value" property is modified, if the calculation changes.

Vectorgebra.Variable.derive()

A helper function used to backpropagate the gradient calculation. Always returns a tuple of 2 tuples, each are also length 2.

First tuple is the partial derivative results of the given node, respect to the first backwards node and second backwards node in order. Second tuple is just the tuple of backwards nodes of the given node, in order.

If backwards is None, returns ((1, 1), (0, 0)).

Vectorgebra.grad(node, args)

This function, is an implementation of autograd functionality on Variable class. The first argument, "node", is assumed to be the top-most node of the graph/tree subject to the gradient calculation. The second argument must be a list of Variable objects. The derivative respect to each Variable object given in this list is calculated, and a collective gradient vector/list is returned. Returned list holds floats, not Variables.

This function builds a separate semi-computational graph, only including the grad values. It basically applies a complete mirroring of the original graph. Without any extra tweak, this function can be called from separate threads in complete safety. The mirroring keeps each threads calculations independent of each other. However, this will consume more memory as the thread count increases.

An example usage is given below:

from vectorgebra import *

# You can also use .toVariable() method to convert the Vector
# to a Variable Vector. Same goes for Matrix objects.
x = Vector(*[Variable(k) for k in range(10000)])
y = Vector.randVfloat(10000, 0, 2)
z = x.dot(y)
print(grad(z, x.values))

Vectorgebra.autograd(node, args)

The mechanism of this function is almost the same as Vectorgebra.grad() above. The usage is the same. Above example code is also valid for this function. And as an extra, "args" can be None for this function.

This function uses the already existing computational graph to calculate the gradients and is more than 2 times faster than "grad()". But it can only be run single threadedly. No form of parallelization is safe with this function.

The mechanism utilizes 2 new properties of the Variable class. "grad" property of a Variable object is meant to store the last calculated gradient value for the given Variable, in the whole runtime. "pass_id" property is a randomly generated 64-bit integer, that is meant to differentiate between "autograd()" calls. Each call to this function, renews firstly the pass_id of the "node" argument, and all the nodes connected to node from below, as backwards.

"grad" property of all Variable objects are updated by this function. At each pass, a "grad" is calculated for each given Variable in the graph, and is saved into the property. When returning the gradient, this function just retrieves this property from all the objects given in "args" list.

If "args" list is empty, then an empty list is returned. But all of the "grad" values in the given computational graph, "node" as the root, is updated. This updated information can later be retrieved by the user manually in any way possible. The "pass_id" of the given gradient calculation can be retrieved from the "pass_id" property of "node" argument.

Parallelization must be avoided with this function, because it will generate a read-write conflict between threads. Multiple threads may try to write to "grad" property of the same object at the same time, or in the wrong order, if managed incorrectly. An improved implementation with a mutex to be thread safe may be developed in the future though.

Vectorgebra.Vector

Includes basic and some sophisticated operations on vectors.

Addition, multiplication subtraction, division and operations alike are implemented as overloads. Comparison operators compare the length of the vectors. Only exception is == which returns True if and only if all the terms of the vectors are equal.

Vector Functions

You can pass functions as elements to Vector objects. If you chose to create vector functions with this method, all elements of the vector must be a function and must accept the same amount of arguments that have no default values. Currently basic arithmetic operations are defined for vector functions. We can say this feature is yet experimental.

Methods are listed below.

Vectorgebra.Vector.dot(v)

Returns the dot product of self with v.

Vectorgebra.Vector.append(v)

Appends the argument to the vector. Returns the new vector. Argument can be int, float or Vector.

Vectorgebra.Vector.copy()

Returns the copy of the vector.

Vectorgebra.Vector.pop(ord)

Functions exactly the same as .pop() for the list class. If left blank, pops the last element and returns it. If specified, pops the intended element and returns it.

Vectorgebra.Vector.length()

Returns the length of self.

Vectorgebra.Vector.proj(v)

Projects self onto v and returns the resulting vector. Due to the division included, may result in inaccurate values that should have been zero. However, these values are very close to zero and are of magnitude 10-17. This situation is important for the method Vector.spanify().

Vectorgebra.Vector.unit()

Returns the unit vector.

Vectorgebra.Vector.spanify(*args)

Applies Gram-Schmidt process to the given list of vectors. Returns the list of resulting vectors. May have inaccuracies explained for Vector.dot() method.

Vectorgebra.Vector.does_span(*args)

Returns True if the given list of vectors span the Rn space where n is the number of vectors. Returns False otherwise. Eliminates the possible error from Vector.spanify() method. Therefore, this method will work just fine regardless the errors from divisions.

Vectorgebra.Vector.randVint(dim , a, b, decimal=False)

Returns dim dimensional vector which has its elements randomly selected as integers within the interval (a, b). If decimal is true, generated contents are decimal objects.

Vectorgebra.Vector.randVfloat(dim, a, b, decimal=False)

Returns dim dimensional vector which has its elements randomly selected as floats within the interval (a, b). If decimal is true, generated contents are decimal objects.

Vectorgebra.Vector.randVbool(dim, decimal=False)

Returns dim dimensional vector which has its elements randomly selected as booleans. If decimal is true, generated contents are decimal objects.

Vectorgebra.Vector.randVgauss(dim, mu, sigma, decimal=False)

Returns dim dimensional vector which has its elements randomly selected on gaussian curve defined by mu and sigma. Uses random.gauss internally. Therefore, limitations of this function should be kept in mind. If decimal is true, generated contents are decimal objects.

Vectorgebra.Vector.determinant(*args)

Returns the determinant of the matrix which has rows given as vectors in *args. This method is not intended for casual use. It is a background tool for cross product and the determinant method for Matrix class.

Vectorgebra.Vector.cross(*args)

Returns the cross product of the vectors given in *args.

Vectorgebra.Vector.outer(v, w)

Returns the outer product of Vectors v and w. Return type is therefore a Matrix.

Vectorgebra.Vector.cumsum()

Returns the cumulative sum.

Vectorgebra.Vector.zero(dim)

Returns dim dimensional zero vector.

Vectorgebra.Vector.one(dim)

Returns dim dimensional all ones vector.

Vectorgebra.Vector.reshape(a, b)

Returns the reshaped matrix.

Vectorgebra.Vector.rotate(i, j, angle, resolution: int = 15)

Rotates the vector, self, around axes "i" and "j" by "angle". "resolution" argument is passed to cos() and sin(). Rotation is done via Givens rotation matrix.

Vectorgebra.Vector.softmax(resolution=15)

Applies softmax operation to self and returns it. "resolution" is passed to e() function internally.

Vectorgebra.Vector.minmax()

Applies MinMax operation to self and returns it.

Vectorgebra.Vector.relu(leak=0, cutoff=0)

Maps self with ReLU function and returns the resultant vector. Put a non-zero leak for leaky ReLU. You can also implement a cutoff for the positive side of the function.

Vectorgebra.Vector.sig(a=1, cutoff=None)

Applies the sigmoid to self and returns the resultant vector. "a" is the coefficient of x, you can implement a cutoff which acts as a radius. Above +cutoff will return 1, below -cutoff will return 0.

Type conversions

.toInt(), .toFloat(), .toBool(), .toDecimal(), .toVariable().

Vectorgebra.Vector.map(f)

Maps the elements of the self according to the function "f".

Vectorgebra.Vector.filter(f)

Filters the elements of self according to the function "f".

Vectorgebra.Vector.sort(reverse=False)

Sorts the vector. This function uses built in sort. "reverse" argument is directly passed into it.

Vectorgebra.Vector.avg()

Returns the average of all numbers in self.


Vectorgebra.Matrix

Includes basic operations for matrices.

Basic operations like addition, multiplication subtraction, division are implemented as overloads. Only comparison operator implemented is == which returns true if and only if all the elements of the matrices are equal.

"pow" method accepts the parameter "decimal".

Methods are listed below.

Vectorgebra.Matrix.determinant(choice="echelon")

Returns the determinant of the matrix m. Two choices are available currently; echelon, analytic. Default is echelon.

Vectorgebra.Matrix.append(arg)

Appends arg as a new row to self. Only accepts vectors as arguments.

Vectorgebra.Matrix.copy()

Returns the copy of the matrix.

Vectorgebra.Matrix.pop(ord)

Functions exactly the same as .pop() in list class. If left blank, pops the last row and returns it. If specified, pops the row in the given order and returns it.

Vectorgebra.Matrix.transpose()

Returns the transpose matrix of self

Vectorgebra.Matrix.conjugate()

Returns the complex conjugate of the self.

Vectorgebra.Matrix.normalize()

Divides self with its determinant.

Vectorgebra.Matrix.hconj()

Returns the Hermitian conjugate of self.

Vectorgebra.Matrix.norm(resolution: int = 15, decimal=False)

Returns the Frobenius norm of self. Utilizes the eigenvalue function. Parameters are directly passed to eigenvalue function.

Vectorgebra.Matrix.inverse(method="iteraitve", resolution=10, lowlimit=0.0000000001, highlimit=100000, decimal=False)

Returns the inverse matrix of self. Returns None if not invertible. method can ben "analytic", "gauss", "neumann" or "iterative". Default is iterative which uses Newton's method for matrix inversion. Resolution is the number of iterations. lowlimit and highlimit are only for gauss method. They control the "resolution" of multiplication and divisions. LU method only uses the decimal choice. See the source code for a better inside look.

Neumann will only work for the right conditioned matrices (see here). Neumann only uses resolution parameter.

LU decomposition is the fastest method amongst all.

Vectorgebra.Matrix.identity(dim, decimal=False)

Returns dimxdim dimensional identity matrix. If decimal is true, generated contents are decimal objects.

Vectorgebra.Matrix.zero(a, b, decimal=False)

Returns axb dimensional all 0 matrix. If decimal is true, generated contents are decimal objects.

Vectorgebra.Matrix.one(a, b, decimal=False)

Returns axb dimensional all 1 matrix. If decimal is true, generated contents are decimal objects.

Vectorgebra.Matrix.randMint(m, n, a, b, decimal=False)

Returns mxn matrix of random integers selected from the interval (a, b). If decimal is true, generated contents are decimal objects.

Vectorgebra.Matrix.randMfloat(m, n, a, b, decimal=False)

Returns mxn matrix of random floats selected from the interval (a, b). If decimal is true, generated contents are decimal objects.

Vectorgebra.Matrix.randMbool(m, n, decimal=False)

Returns mxn matrix of random booleans. If decimal is true, generated contents are decimal objects.

Vectorgebra.Matrix.randMgauss(m, n, mu, sigma, decimal=False)

Returns mxn matrix of random values on the gaussian curve described by mu and sigma. If decimal is true, generated contents are decimal objects. Beware of the limitations of random.gauss for this method.

Vectorgebra.Matrix.echelon()

Returns reduced row echelon form of self. Also does reorganization on rows and multiplies one of them by -1 every 2 reorganization. This is for the determinant to remain unchanged.

Vectorgebra.Matrix.cramer(a, number)

Applies Cramers rule to the equation system represented by the matrix a. number indicates which variable to calculate.

Vectorgebra.Matrix.cumsum()

Returns the cumulative sum.

Vectorgebra.Matrix.reshape(*args)

Returns the reshaped matrix/vector. If the return is a matrix, makes a call to the vectors reshape.

Vectorgebra.Matrix.eigenvalue(resolution: int)

Calculates the eigenvalues of self and returns a list of them. This function cannot calculate complex valued eigenvalues. So if there are any, there will be incorrect numbers in the returned list instead of the complex ones.

The underlying algorithm is QR decomposition and iteration. Resolution is the number of iterations. Default is 10.

Vectorgebra.Matrix.lu(decimal: bool = False)

Returns a tuple of L and U matrices, in order, which are the respective LU decompositions of self. This method does not check for PA = LU cases, blindly applies the decomposition. Returned L and U matrices may multiply to not A, but possibly a PA matrix.

Vectorgebra.Matrix.qr()

Applies QR decomposition to self and returns the tuple (Q, R). The algorithm just uses .spanify() from Vector class. If the columns of self do not consist of independent vectors, returns matrices of zeroes for both Q and R. This is to prevent errors that may have otherwise risen from written code.

Vectorgebra.Matrix.cholesky()

Applies Cholesky decomposition to self, and returns the L matrix. Applied algorithm is textbook Cholesky–Banachiewicz algorithm.

Vectorgebra.Matrix.get_diagonal()

Returns the diagonal Matrix such that A = L + D + U.

Vectorgebra.Matrix.get_upper()

Returns the upper triangular Matrix such that A = L + D + U.

Vectorgebra.Matrix.get_lower()

Returns the lower triangular Matrix such that A = L + D + U.

Vectorgebra.Matrix.givens(dim, i, j, angle, resolution: int = 15)

Returns the Givens rotation matrix that applies rotation around axes "i"-"j" by "angle". Matrix is dimxdim dimensional. "resolution" is passed to cos() and sin()

Vectorgebra.Matrix.frobenius_product(a, b)

Returns the Frobenius product of matrix a and matrix b.

Vectorgebra.Matrix.trace()

Returns the trace of self.

Vectorgebra.Matrix.diagonals()

Returns the list of diagonals.

Vectorgebra.Matrix.diagonal_mul()

Returns the multiplication of diagonals.

Vectorgebra.Matrix.gauss_seidel(b: Vector, initial=None, resolution=15, decimal=False)

Applies Gauss-Seidel method to the equation self * x = b. Returns the resultant x Vector. If "initial" left unchanged, code creates an initial guess by default. This may not converge though. "resolution" is the number of iterations. "decimal" argument is also passed to called functions inside this method. Same for resolution.

Vectorgebra.Matrix.least_squares(b, *args)

Accepts every argument that .inverse() accepts. Solves the equation self * x = b for x. Every argument except b is passed to .inverse().

Vectorgebra.Matrix.jacobi_solve(b, resolution: int = 15)

Solves the equation self * x = b for x via Jacobi algorithm. "resolution" is the number of iterations. Returns the x vector.

Type conversions

.toInt(), .toFloat(), .toBool(), .toDecimal(), toVariable().

Vectorgebra.Matrix.map(f)

Maps the elements of self according to the function "f".

Vectorgebra.Matrix.filter(f)

Filters the elements of self according to the function "f".

Attention! This method is very prone to DimensionError. Be sure that for each row, after the filtering, the same amount of elements get through.

Vectorgebra.Matrix.submatrix(a, b, c, d)

Slices the self by rows a:b, by columns c:d and returns the resulting matrix.

Vectorgebra.Matrix.avg()

Returns the average of all elements in self.


Vectorgebra.Tensor

A Tensor, is a container which contains either matrices or tensors. Each element that it contains, has one less dimension factor than the parent. This wrapper therefore creates a recursive data structure of, eventually, vectorgebra.Matrix. Matrix is the last recursive step of this container.

Vectorgebra.Tensor.dot(arg)

Returns the dot product of self and arg. arg must be another Tensor. Dimensions have to match.

Vectorgebra.Tensor.append(arg)

Appends the Tensor arg to self. Dimensions need to be matching.

Vectorgebra.Tensor.pop(ord=-1)

Pops the indexed element from the top most layer of the Tensor.

Vectorgebra.Tensor.copy()

Returns the deep copy of self.

Vectorgebra.Tensor.zero(dim, decimal=False)

Returns an all zero Tensor with the dimensions specified in "dim".

Vectorgebra.Tensor.one(dim, decimal=False)

Returns an all one Tensor with the dimensions specified in "dim".

Vectorgebra.Tensor.identity(dim, decimal=False)

Returns the identity Tensor with the dimensions specified in "dim". The generated identity here has only ones in the all-dimensional diagonal. The filled diagonal is always a "line", not "plane", etc.

Vectorgebra.Tensor.map(f)

Maps the self with function f. Returns the Tensor filled with resultant values.

Vectorgebra.Tensor.avg()

Returns the average of all numbers in self.

Vectorgebra.Tensor.flatten()

Flattens the Tensor. If more than 2-dimensional, returns a Matrix filled with average values of higher dimensional parts in the original Tensor. If not, just returns a deep copy of self.


Vectorgebra.Graph

The class for graphs. These can be constructed via matrices or manually. Can be both directed or undirected.

Constructor accepts 5 arguments; vertices, edges, weights, matrix, directed. "directed" is a bool and False by default. If "matrix" is given, "edges" and "weights" are ignored. If given, "vertices" is not ignored. If left blank, vertices are named numerically. Matrix must be a square, obviously.

If manually constructed; "vertices" is the list (or tuple) of vertices names. Items can be anything that is hashable. "edges" is a list (or tuple) of length-2 lists (or tuples). Both items must be valid names of vertices, also named in the "vertices" argument. Weights to these edges are passed in-order from "weights" list if is not None. If "weights" is left blank, 1 is assigned as weight to given edges. The adjacency matrix is always generated.

Related data can be accessed through; self.vertices, self.edges, self.weights, self.matrix, self.directed.

Print method is overloaded. Printing is much better than matrices even though it kind of prints the adjacency matrix.

Vectorgebra.Graph.addedge(label, weight=1)

Add an edge with vertex pair "label". Weight is 1 by default. Returns self.

Vectorgebra.Graph.popedge(label)

Pops the edge and returns it defined by "label". If there is more than one, pops the first instance.

Vectorgebra.Graph.addvertex(v)

Adds a vertex named "v". Adjacency matrix is regenerated here.

Vectorgebra.Graph.popvertex(v)

Removes the vertex named "v" and returns it. Removes all edges connected to vertex "v". Adjacency matrix is naturally regenerated.

Vectorgebra.Graph.getdegree(vertex)

Returns the degree of vertex.

Vectorgebra.Graph.getindegree(vertex)

Returns the in-degree of vertex if the graph is directed. Otherwise just returns the undirected degree.

Vectorgebra.Graph.getoutdegree(vertex)

Returns the out-degree of vertex if the graph is directed. Otherwise just returns the undirected degree.

Vectorgebra.Graph.getdegrees()

Returns a dictionary of degrees and vertices. Keys are degrees, values are corresponding vertices' labels.

Vectorgebra.Graph.getweight(label)

Returns the weight of the edge given via label.

Vectorgebra.Graph.isIsomorphic(g, h)

Returns True if g and h are isomorphic, False otherwise. g and h must be Graphs.

Vectorgebra.Graph.isEuler()

Returns True if self is an Euler graph, False otherwise.


Constants

Pi, e, log2(e), log2(10), ln(2), sqrt(2), sqrt(pi), sqrt(2 * pi).

Functions

Vectorgebra.Range(low, high, step)

A lazy implementation of range. There is indeed no range. Just a loop with yield statement. Almost as fast as the built-in range.

Vectorgebra.abs(arg)

Returns the absolute value of the argument.

Vectorgebra.sqrt(arg, resolution: int = 10)

A square root implementation that uses Newton's method. You may choose the resolution, but any change is not needed there. Pretty much at the same accuracy as the built-in math.sqrt(). Accepts negative numbers too.

Vectorgebra.cumsum(arg: list or float)

Returns the cumulative sum of the iterable.

Vectorgebra.__cumdiv(x, power: int)

Calculates xn / power!. This is used to calculate Taylor series without data loss (at least minimal data loss).

Vectorgebra.e(exponent, resolution: int)

Calculates eexponent. resolution is passed as power to the __cumdiv(). It will then define the maximal power of the power series implementation, therefore is a resolution.

Logarithms

There are 4 distinct logarithm functions: log2, ln, log10, log. Each have the arguments x and resolution. "resolution" is the number of iterations and by default is set to 15. "log" function also takes a "base" parameter.

All logarithms are calculated based on "log2" function. "ln" and "log10" use the related constants instead of recalculating the same value over and over again.

Vectorgebra.sigmoid(x, a=1)

Returns the sigmoid functions value at x, where a is the coefficient of x.

Vectorgebra.ReLU(x, leak=0, cutoff=0)

Applies rectified linear unit with leak and cutoff given to x.

Vectorgebra.Sum(f, a, b, step=0.01, control: bool=False, limit=0.000001)

Returns the sum of f(x) from a to b. step is the step for Range. If control is true, stops the sum when the absolute value of the derivative drops under "limit".

Vectorgebra.mode(data)

Returns the mode of the data. Tuples, lists, vectors and matrices are accepted.

Vectorgebra.mean(data)

Calculates the mean of data. "data" must be a one dimensional iterable.

Vectorgebra.median(data)

Returns the median of data. "data" must be a one dimensional iterable.

Vectorgebra.expectation(values, probabilities, moment: int = 1)

"values" and "probabilities" are one dimensional iterables and their lengths must be equal. There is no value checking for the probabilities. If they sum up to more than 1 or have negative values, it is up to the user to check that. "moment" is the power of "values". Returns the expectation value of given data.

Vectorgebra.variance(values, probabilities)

Same constraints as "expectation" apply here. Returns the variance of the given data.

Vectorgebra.sd(values, probabilities)

Same constraints as "variance" apply here. Returns the standard deviation of the given data.

Vectorgebra.maximum(dataset)

Returns the maximum value of dataset. Dataset can be anywhere from tuples to Matrices.

Vectorgebra.minimum(dataset)

Returns the minimum value of dataset. Dataset can be anywhere from tuples to Matrices.

Vectorgebra.unique(data)

Returns a dictionary that has unique elements as keys and counts of these elements appearances in "data" as values. "data" must be an iterable.

Vectorgebra.isAllUnique(data)

Returns True if all elements in data are unique. "data" must be an iterable.

Vectorgebra.permutate(sample)

Returns all permutations of "sample" in a list. Only unique elements are counted in "sample". This function utilizes helper function "Vectorgebra.__permutate()". "sample" must be an iterable.

Vectorgebra.factorial(x: int)

Calculates the factorial with recursion. Default argument is 1.

Vectorgebra.permutation(x: int, y: int)

Calculates the y permutations of x elements. Does not utilize the factorial function. Indeed, uses loops to calculate the aimed value more efficiently.

Vectorgebra.combination(x: int, y: int)

Calculates y combinations of x elements. Again, this does not utilize the factorial function.

Vectorgebra.multinomial(n: int, *args)

Calculates the multinomial coefficient with n elements, with partitions described in "args". Does not utilize the factorial.

Vectorgebra.binomial(n: int, k: int, p: float)

Calculates the probability according to the binomial distribution. n is the maximum number of events, k is the events that p describes, p is the probability of the "event" happening.

Vectorgebra.geometrical(n: int, p: float)

Calculates the probability according to the geometric distribution. n is the number of total events. p is the probability that the event happens.

Vectorgebra.poisson(k, l)

Calculates the probability according to the Poisson formula. l is the lambda factor. k is the "variable" on the whatever system this function is used to describe.

Vectorgebra.normal(x, resolution: int = 15)

Calculates the normal gaussian formula given x. "resolution" is directly passed to the e() function.

Vectorgebra.gaussian(x, mean, sigma, resolution: int = 15)

Calculates the gaussian given the parameters. "resolution" is directly passed to the e() function.

Vectorgebra.laplace(x, sigma, resolution: int = 15)

Calculates the Laplace distribution given the parameters. "resolution" is directly passed to the e() function.

Vectorgebra.linear_fit(x, y, rate=0.01, iterations: int = 15)

Returns the b0 and b1 constants for the linear regression of the given data. x and y must be one dimensional iterables and their lengths must be equal. "rate" is the learning rate. "iterations" is the total number of iterations that this functions going to update the coefficients.

Vectorgebra.general_fit(x, y, rate=0.0000002, iterations: int = 15, degree: int = 1)

Calculates the coefficients for at degree polynomial regression. Default rate argument is much much lower because otherwise result easily blows up. Returns the coefficients starting from the zeroth degree as a Vector object.

Internally, x and y sets are converted to Vectors if they were not, so it is faster to initialize them as Vectors.

Vectorgebra.kmeans(dataset, k=2, iterations=15, a = 0, b = 10)

Applies the K-means algorithm on the dataset. "k" is the number of points to assign data clusters. "iterations" is the number of iterations that the algorithm applies. Dataset must be non-empty. Each row of dataset is converted to Vectors internally. Predefining them as such would make the function faster.

Every element of dataset must be of the same type whether they are Vectors or not. Type-checking is based on the first row of the dataset.

Returns a 2-element tuple. First element is a list of Vectors which point to the cluster centers. Second element is a list of lists of Vectors which consist of the initial data. This list has the same length as number of generated cluster centers. Each internal list corresponds to the same indexed center point. So this data is grouped by cluster centers.

Initial guesses are random points whose components are random floats between a and b.

This function does not have decimal support yet.

Trigonometrics

All are of the format Vectorgebra.name(x, resolution: int). Calculates the value of the named trigonometric function via Taylor series. Again, resolution is passed as power to __cumdiv().

Inverse trigonometrics(arcsin, arccos) do not use the helper function __cumdiv().

Vectorgebra.__find(...)

This is a helper function for Math.solve(). Arguments are the same. Returns the first zero that it finds and saves it to memory.

Vectorgebra.solve(f, low, high, search_step, res)

Finds zeroes of function f. It may not be able to find all zeroes, but is pretty precise when it finds some. If the functions derivative large around its zero, then you should increase resolution to do a better search.

Retrieves found zeroes from the memory, then clears it. Calling multiple instances of this function at the same time will result in errors because of this global memory usage.

This function is optimized for polynomials. It doesn't matter how many zeroes they have since this function utilizes a thread pool. This solver is slow when utilized with Taylor series based functions.

There is an obvious solution to this speed problem though. Just put expanded form as the argument. Not the implicit function form.

Exits the main loop if the maximum thread count is reached. This can be used as a limiter when given b=Infinity(). However, the thread count limit is most likely 4096.

Vectorgebra.derivative(f, x, h)

Takes the derivative of f around x with h = h. There is no algorithm here. It just calculates the derivative.

Vectorgebra.integrate(f, a, b, delta)

Calculates the integral of f(x) in the interval (a, b) with the specified delta. Default for delta is 0.01. Uses the midpoint rule.

Vectorgebra.__mul(...)

A helper function to Vectorgebra.matmul(). Threads inside matmul call this function.

Vectorgebra.matmul(m1, m2, max=10)

Threaded matrix multiplication. Its speed depends on dimensions of given matrices. Let it be axb and bxc, (a - b) is proportional to this functions speed. Worst case scenario is square matrices. 44x44 (On CPython) is limit for this function to be faster than the normal version of matrix multiplication.

If b > a, normally this function gets even more slower. But there is a way around. Let it be b > a;

A * B = C

BT * AT = CT

After taking the transposes, we get a > b again. All we have to do is to calculate the matrix CT instead of C directly then to calculate the transpose of it.

(I didn't add this function to Matrix class because I have more plans on it.)

Vectorgebra.findsol(f, x, resolution)

Calculates a single solution of f with Newton's method. x is the starting guess. resolution is the number of iterations.

Vectorgebra.Complex

This is the complex number class. It has + - / * overloaded.

Vectorgebra.Complex.conjugate()

Returns the complex conjugate of self.

Vectorgebra.Complex.length()

Returns the length of self, with treating it as a vector.

Vectorgebra.Complex.unit()

Treats the complex number as a vector and returns the unit vector.

Vectorgebra.Complex.sqrt(arg, resolution: int = 200)

Calculates the square root of the complex number arg and returns it again, as a complex number. Resolution argument is only passed to arcsin since it is the only limiting factor for this functions accuracy. Has an average of 1 degree of error as angle. You may still increase the resolution. But reaching less than half a degree of error requires for it to be at least 600.

The used algorithm calculates the unit vector as ei*x. Then halves the degree, x. Returns the resultant vector at the proper length.

Vectorgebra.Complex.range(lowreal, highreal, lowimg, highimg, step1, step2)

Creates a complex number range, ranging from complex(lowreal, lowimg) to complex(highreal, highimg). Steps are 1 by default. Again this is a lazy implementation.

Vectorgebra.Complex.inverse()

Returns 1 / self. This is used in division. If divisor is complex, then this function is applied with multiplication to get the result.

Vectorgebra.Complex.rotate(angle: int or float)

Rotates self by angle.

Vectorgebra.Complex.rotationFactor(angle: int or float)

Returns ei*angle as a complex number.


Vectorgebra.Infinity

The class of infinities. When initialized, takes one argument describing its sign. If "True", the infinity is positive (which is the default). If "False", the infinity is negative.

Logical and mathematical operations are all overloaded. Operations may return "Undefined". This is a special class that has every mathematical and logical operation overloaded.

Vectorgebra.Undefined

A special class that corresponds to "undefined" in mathematics.

Exceptions

Vectorgebra.DimensionError

Anything related to dimensions of vectors and matrices. Raised in Vector class when dimensions of operands don't match or 0 is given as a dimension to random vector generating functions.

This error is raised in Matrix class when non-square matrices are passed into inverse calculating functions.

DimensionError(1) has been changed to RangeError, but is still in the code.

Vectorgebra.ArgTypeError

Anything related to types of arguments. Can take in a str argument flagged as "hint".

Vectorgebra.RangeError

Raised when given arguments are out of required range.

Vectorgebra.AmountError

Raised when the amount of arguments in a function is wrong.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vectorgebra-3.4.0.tar.gz (79.7 kB view details)

Uploaded Source

Built Distribution

vectorgebra-3.4.0-py3-none-any.whl (79.4 kB view details)

Uploaded Python 3

File details

Details for the file vectorgebra-3.4.0.tar.gz.

File metadata

  • Download URL: vectorgebra-3.4.0.tar.gz
  • Upload date:
  • Size: 79.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for vectorgebra-3.4.0.tar.gz
Algorithm Hash digest
SHA256 210ba977d3b800b2293aa2ec2ffd3f280b7c1b32f4f3446a078737291151ce4c
MD5 9080f26d64089d3d99a2335af3a00132
BLAKE2b-256 a5a59f20fcebd80aaecb342adb73c1886d7068496b9dd3e5139fa72d10905603

See more details on using hashes here.

File details

Details for the file vectorgebra-3.4.0-py3-none-any.whl.

File metadata

  • Download URL: vectorgebra-3.4.0-py3-none-any.whl
  • Upload date:
  • Size: 79.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for vectorgebra-3.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e503422e921f24f24caff6aa287ec7b03ab9796e34f6b3ce818ea9bb68dac7c
MD5 deb0f81190ac0d34965d4075b41133f7
BLAKE2b-256 3442121ca74b189fc5d064a5c06b538418edcfbd2cfceb7f8b9c6f6262d51d2e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page