FAQ Database Discussion Community


how to solve many overdetermined systems of linear equations using vectorized codes?

multidimensional-array,scipy,vectorization,linear-algebra,least-squares
I need to solve a system of linear equations Lx=b, where x is always a vector (3x1 array), L is an Nx3 array, and b is an Nx1 vector. N usually ranges from 4 to something like 10. I have no problems solving this using scipy.linalg.lstsq(L,b) However, I need to...

In-place elementwise matrix multiplication aka Schur Product aka Hadamard Product?

linear-algebra,julia-lang
I'm looking for an in-place implementation of elementwise matrix multiplication in Julia, aka the Schur Product aka the Hadamard product. It can be performed with allocations by A .* B, but I cannot allocate additional memory every time I perform this operation. Of course I could implement it myself, but...

source code of GNU Octave function: mldivide

octave,linear-algebra
Where can I find source code of mldivide of GNU Octave? Is it implemented as C++ code or is it m-file? Typing which mldivide in GNU Octave command line returns mldivide is a built-in function. I searched but I couldn't find the source code.

Array not changing after being passed through void function

c,arrays,matrix,linear-algebra,numerical-methods
I've written a program in C to compute the SVD of a matrix, using Jacobi's algorithm. I've got 3 functions in the program: a function called jacobi_helper which will perform the SVD, a function called jacobi which calls jacobi_helper after initializing a few arrays, and the main function which calls...

How to remove rows while iterating in numpy

python,numpy,matrix,linear-algebra
How to remove rows while iterating in numpy, as Java does: Iterator < Message > itMsg = messages.iterator(); while (itMsg.hasNext()) { Message m = itMsg.next(); if (m != null) { itMsg.remove(); continue; } } Here is my pseudo code. Remove the rows whose entries are all 0 and 1 while...

Find number of rows/columns in a GSL matrix?

c++,matrix,linear-algebra,gsl
Say I have some gsl_matrix * A. I want to write a function that retrieves e.g. the number of rows in this matrix, without having access to anything else besides the object A itself. Example: int num_rows(gsl_matrix * A){ //some operation(s) on A that find the number of rows in...

Bitwise operations on Armadillo vectors

c++,linear-algebra,armadillo
I need to do bitwise operations between two vectors. I dont want to use loops. I am using Armadillo. Could anyone help me with this? Edit: By vector I mean a vector template provided by Armadillo library. Something like this: urowvec newvec1 = zeros < urowvec >(5); urowvec newvec2 =...

Test if points lie on a 2D fitted plane in 4D

matlab,vector,geometry,linear-algebra
Suppose I have 3+ coplanar but not collinear points in R^4. To find the 2D plane (not hyperplane) in which they all lie, I used the following plane fit algorithm from MatlabCentral: function [n,V,p] = affine_fit(X) % Computes the plane that fits best (least square of the normal distance %...

doing algebra with an MxNx3 array using vectorization in python?

arrays,vector,scipy,vectorization,linear-algebra
Suppose I have an MxNx3 array A, where the first two indexes refer to the coordinates a point, and the last index (the number '3') refers to the three components of a vector. e.g. A[4,7,:] = [1,2,3] means that the vector at point (7,4) is (1,2,3). Now I need to...

Linear algebra on python (gaussian elimination)

python,linear-algebra
I am a newbie to python and I came across some issues while I was working on Gaussian elimination using python. I got the error message: IndexError: index 4 is out of bounds for axis 0 with size 4. How should I correct it? A = array( [[3,-1,-1,0,0,0], [-1,4,-1,-1,0,0], [0,0,-1,-1,4,-1],...

inverse of randomly generated binary matrix in matlab

matlab,linear-algebra
I have generated binary non singular matrix of size 602*602 l = 602; w = round (rand (l,l)); now I want to find the inverse of this matrix in binary form, I tried this command w_inv = mod(inv(w),2) but the result is not in binary form. HELP!...

Count of matching values in matrix columns

r,matrix,linear-algebra,matrix-multiplication,transpose
I'm not really sure how to word this correctly but I would like to count the number of times there are similarities among the columns. Imagine I have the 3 NFL teams listed below. Zeroes are losses and ones are victories. The rows are the week of the NFL season....

TypeError: zip argument #1 must support iteration (Vector sum for Ipython)

python,vector,ipython,linear-algebra,ipython-notebook
Hi I'm following "Data Science from Scratch" and I got an error when I was making vector sum function. Could someone help? The code: a = [1,2,3] b = [2,3,4] def vector_add(v, w): """adds two vectors componentwise""" return [v_i + w_i for v_i, w_i in zip(v,w)] vector_add(a,b) #prints [3, 5,...

Back face culling for linestrips

opengl,directx,shader,linear-algebra
I have circle in 3D space (red on image) with normals (white) This circle is being drawn as linestrip. Problem is: i need to draw only those pixels whose normals directed into camera (angle between normal and camera vector is < 90) using discard in fragment shader code. Like backface...

Matrix Low Rank Approximation using Matlab

matlab,matrix,linear-algebra,svd
Consider a 256 x 256 matrix A. I'm familiar with how to calculate low rank approximations of A using the SVD. Typically after using [U S V] = svd(A), I would use Ak = U(:,1:k)*S(1:k,1:k)*V(:,1:k)'; to get the rank k approximation of A. My question is how do I create...

How does this Matlab/Octave code create a Boolean matrix from a vector?

arrays,matlab,matrix,octave,linear-algebra
The first line of code creates some vector with "discrete labels", and the second line of code creates a sparse matrix with ones at the index that the label represents. "eye" creates an identity matrix, but then even if the vector "a" is much longer, this effect of creating a...

Incorrect eigenvalues SciPy sparse linalg.eigs, eigsh for non-diagonal M matrix

scipy,linear-algebra,sparse,eigenvalue
Why do eigh and eigsh from scipy.sparse.linalg as used below give incorrect results when solving the generalized eigenvalue problem A * x = lambda * M * x , if M is non-diagonal? import mkl import numpy as np from scipy import linalg as LA from scipy.sparse import linalg as...

Multilayer Perceptron replaced with Single Layer Perceptron

math,machine-learning,neural-network,linear-algebra,perceptron
I got a problem in understending the difference between MLP and SLP. I know that in the first case the MLP has more than one layer (the hidden layers) and that the neurons got a non linear activation function, like the logistic function (needed for the gradient descent). But I...

Solving a linear system of equation with two variables in MATLAB

matlab,linear-algebra,equation-solving,bisection
It might seem a simple question. I need it, though. Let's assume we have two equations: 2 * y + x + 1 = 0 and y - 2 * x = 0 I would like to find their bisection which can be calculated from this equation: |x + 2...

Different results of SVD (Singular values decomposition)

c,matlab,linear-algebra
I am trying to convert a matlab code to C. The matlab code uses a singular value decomposition (SVD) of 3x3 matrices that I implemented in C using numerical reciepes. The matlab code works later with the right singular vectors wich are in some cases that I tested different between...

Why does cuSOLVER cusolverSpDcsrlsvchol not work?

c++,cuda,linear-algebra,solver,cusolver
We are experiencing problems while using cuSOLVER's cusolverSpScsrlsvchol function, probably due to misunderstanding of the cuSOLVER library... Motivation: we are solving the Poisson equation -divgrad x = b on a rectangular grid. In 2 dimensions with a 5-stencil (1, 1, -4, 1, 1), the Laplacian on the grid provides a...

Finding a vector that is approximately equally distant from all vectors in a set

python,vector,linear-algebra,mathematical-optimization,approximate
I have a set of 3 million vectors (300 dimensions each), and I'm looking for a new point in this 300 dim space that is approximately equally distant from all the other points(vectors) What I could do is initialize a random vector v, and run an optimization over v with...

How would I implement MATLAB's “eig(A, B)” function in JavaScript

javascript,matlab,linear-algebra,lapack
According to MATLAB's documentation: [V,D] = eig(A,B) returns diagonal matrix D of generalized eigenvalues and full matrix V whose columns are the corresponding right eigenvectors, so that A*V = B*V*D. As I read available source code (It seems all implementations I've looked at Octave, R, Scipy) lead to LAPACK's DGGEV...

Solving a simple linear equation

algorithm,linear-algebra,linear
Suppose I needed to solve the following equation, ax + by = c Where a, b, and c are known values and x, y are natural numbers between 0 and 10 (inclusively). Other than the trivial solution of, for (x = 0; x <= 10; x++) for (y = 0;...

Can somebody explain to me what 'void postConcat' in Android does?

android,linear-algebra
So I have 2 matrices. Colormatrix and Threshold. Colormatrix is initially, C = [ 0.213 0.715 0.072 0 0; 0.213 0.715 0.072 0 0; 0.213 0.715 0.072 0 0; 0 0 0 1 0;] and threshold is: T = [ 255 0 0 1 -306; 0 255 0 1 -306;...

Finding upper/lower triangular form of arbitrary matrix n*n - python

python,arrays,numpy,matrix,linear-algebra
every matrix can be written in upper or lower triangular form simply just by rotating the basis. Is there a simple routine in python (numpy) to do it? I was unable to find it and I cant believe that there is no such thing. To ilustrate it: matrix = numpy.array([[a,b,c],...

Rewriting Matlab eig(A,B) (Generalized eigenvalues/eigenvectors) to C/C++

c++,matlab,linear-algebra,eigen,intel-mkl
Do anyone have any idea how can I rewrite eig(A,B) from Matlab used to calculate generalized eigenvector/eigenvalues? I've been struggling with this problem lately. So far: Matlab definition of eig function I need: [V,D] = eig(A,B) produces a diagonal matrix D of generalized eigenvalues and a full matrix V whose...

Python numpy/scipy eigenvectors seemingly not correct for markov chain model

python,numpy,linear-algebra,eigenvector,markov-chains
I have a large (351,351) numpy transition matrix. I would like to find the state state vector for this using numpy (I also tried scipy which has the same exact function). sstate = np.linalg.eig(T)[1][:,0] So this I believe should give me the eigenvector for the dominant left eigenvalue. The dominant...

how to calculate the norm of a vector in a large mxnx3 array?

python,arrays,scipy,vectorization,linear-algebra
Suppose I have an array of the shape (m,n,3), where m and n refers to the y and x coordinates of a point, and the 3 numbers in each point refer to a three-dimensional vector. (A similar situation is an image with height m and width n, and 3 refers...

How to quickly determine if a matrix is a permutation matrix

python,numpy,matrix,linear-algebra
How to quickly determine if a square logical matrix is a permutation matrix? For instance, is not a permutation matrix since the 3rd row have 2 entries 1. PS: A permutation matrix is a square binary matrix that has exactly one entry 1 in each row and each column and...

Representing Travelling Salesman as Linear Expression

algorithm,linear-algebra,linear-programming,cplex,traveling-salesman
I've seen online that one can write the travelling salesman problem as a linear expression and compute it using software such as CPLEX for java. I have a 1000 towns and need to find a short distance. I plan on partitioning these 1000 towns into clusters of ~100 towns and...

Slowing an object in 3D space using vectors

opengl,math,linear-algebra
I am programming a fairly simple 3D minigolf game using OpenGl. I've run across a problem of slowing a minigolf ball that is rolling on a surface. The ball is described by velocity and position vectors. When it moves on a flat surface, it must be slowed, but only in...

Armadillo equivalent for MATLAB operations

c++,matlab,linear-algebra,armadillo
Hi I am new to armadillo. I am looking for the Armadillo equivalent for the following operation performed in MATLAB: B(B_t>=0) = 1; B(B_t<0) = -1; Here B and B_t are two matrices having same dimensions. I can use the armadillo signum function(sign) here but then I need to set...

How to get plane-vertices by a point and normal

c++,math,linear-algebra,glm
I'm having a point p and a normal n (in 3d space). I want to calculate 4 vertex-points which are lying on the plane which is perpendicular to the normal and lies on the point p. The vertices should form a rectangle and p should be the center of this...

How can I determine which routines MATLAB uses to solve a sparse matrix?

algorithm,matlab,linear-algebra,sparse-matrix
I'm trying to solve a sparse matrix equation of the form A*x = b, where A is a known square, sparse matrix, and b is a known column vector, and x is the column vector to be determined. Standard MATLAB syntax for solving this is: x = A\b; Behind the...

How to find linearly independent rows from a matrix

python,numpy,matrix,linear-algebra
How to identify the linearly independent rows from a matrix? For instance, The 4th rows is independent. By the way, the question what I really want to track is here....

Can I use the OpenCV Mat like a normal matrix?

opencv,linear-algebra
Can I use the opencv Mat like a normal matrix for multiply them, sum them and other things. Like linear algebra. I tried but the compiler throws a Opencv's Exception....

Android Rotation Vector and Rotation Matrix

android,opengl-es,linear-algebra,android-sensors,rotational-matrices
So I previously asked a question more about what ind of sensor to use for the application I'm building. After some research I know now that it's the rotation vector that I want to use for I want to be able to move a virtual cube on my tablet according...

What does three.js's Matrix4.multiply() method do?

javascript,matrix,three.js,linear-algebra,matrix-multiplication
I'm learning three.js and am trying to multiply two translation matrices together to get a single matrix that describes both translations: | 1 0 0 -2 | | 1 0 0 3 | | 0 1 0 7 | * | 0 1 0 4 | | 0 0 1...

Singular value decomposition approximation

matlab,linear-algebra,svd
I was asked in school to do a SVD on the matrix: A = [1 3 1 2; 0 2 1 4; 6 5 2 1] and then: calculate an approximation of A called A_hat by setting the third singular value σ_3 to zero. I have done the SVD, but...

multiply each matrix slice in a 3d array by a single matrix in R

arrays,r,matrix,linear-algebra
Suppose I have a 1 x matrix mat=matrix(1,1,13) I also have an array that is 13 x 1000 x 10. dfarray = array(1:(13*1000*10),dim=c(13,1000,10)) Without looping, I want to return the results of this loop dfarray2=array(NA,dim=c(1,1000,10)) for(i in 1:10){ dfarray2[,,i]=mat%*%dfarray[,,i] } ...

Linear equation solver Arduino,printing wrong answers

c,matrix,arduino,calculator,linear-algebra
Please help me any help will be appreciated. I wrote a code in C to solve equations, but I don't know why some equations that I input into this calculator, give me wrong values, the algorithm is correct because I execute it in C and give me the correct answers...

Solving sparse linear system in Matlab

matlab,linear-algebra,sparse-matrix
I have to solve the linear system A*x = b where A is a sparse matrix (34k x 34k). I tried to solve using x = b \ A but this gives me an out of memory error. Is there a better way to solve this system in Matlab?

A_ldiv_B! with sparse matrices

optimization,linear-algebra,julia-lang,factorization,levenberg-marquardt
The following lines of code appear in levenberg-marquardt algorithm in the optimization package "Optim": DtD = diagm(Float64[max(x, MIN_DIAGONAL) for x in sum(J.^2,1)]) delta_x = ( J'*J + sqrt(lambda)*DtD ) \ -J'*fcur However, my questions has nothing to do with the algorithm or anything specific to the package. I guess it...

Matrix power for sparse matrix in python

python,numpy,scipy,linear-algebra,sparse-matrix
I am trying to find out a way to do a matrix power for a sparse matrix M: M^k = M*...*M k times where * is the matrix multiplication (numpy.dot), and not element-wise multiplication. I know how to do it for a normal matrix: import numpy as np import scipy...

Sparse matrix of variable movement (digits) between 2 same sized lists

python,numpy,scipy,linear-algebra,sparse-matrix
I want to create a sparse matrix of the differences between the indexes of two 1D arrays or lists of digits. These two rows give us the positions at time 'a' and at a later time 'b'. a = [6,3,10,2,5,7,4,11,8,9] b = [10,3,6,5,11,2,7,8,9,4] As you can see, '6' has moved...

Divide every slice of a matrix in an array by its own vector?

arrays,r,matrix,linear-algebra
Suppose I have two arrays (or tensors if tensor package is needed) dim(Xbeta) products draws Households 13 20 10 dim(denom) 1 20 10 set.seed(1) Xbeta=array(rnorm(13*20*10,0,1),dim=c(13,20,10)) denom=array(rnorm(1*20*10,0,1),dim=c(1,20,10)) Without looping, I want to do the following: for(i in 1:10){ Xbeta[,,i]=t(t(Xbeta[,,i]) / denom[,,i]) } I want to to divide each column in Xbeta[,,i]...

How can I use numpy.linalg.matrix_power in python to raise a matrix to a large power?

python,numpy,matrix,scipy,linear-algebra
I am trying to raise a matrix to a high power in python/numpy. It seems that for large exponents, the results are not correct(some sort of overflow?), above 10000. Is this a known behavior of the numpy.linalg.matrix_power function or am I missing something? Here is the code I am trying...

Drazin inverse of a matrix

matlab,matrix,linear-algebra
Is there an algorithm that computes the Drazin inverse of a singular matrix? I would like to apply it either in MATLAB or Mathematica.

Intersection between two boxes in 3D space

3d,collision-detection,linear-algebra
I want to implement a collision detection system for my graphic engine. I don't know if it is the common way to do it but my idea was to bound any solid object (like a mesh or the camera) within a 3D box, which would give me much more accurate...

Solving linear equation with LAPACKE

c,linear-algebra,lapack,lapacke
I'm trying to solve some linear equation (which is symmetrical, tridiagonal and positive). I have to use LAPACKE. My code is as follows: #include <lapacke.h> #include <stdio.h> void print_mtrx(double * mtrx, int n, int m) { int i, j; for(i = 0; i < n; i++) { for(j = 0;...

How to express c[i,j,k] = a[i,j] * b[i,k] in Numpy/Theano?

numpy,linear-algebra,theano
The definition c[i,j,k] = a[i,j] * b[i,k] for any i, j, k is an element-wise product with respect to i, and an outer product with respect to j and k. Is there any way to express this in NumPy/Theano without loops?...

Pythonic Way to Solve this Matrix

python,numpy,matrix,linear-algebra
I've been thinking on this problem, but I can't seem to wrap my head around it. I want to solve a matrix with three equations with unknowns x, y, z so they all equal the same number. Lets say my equations are: x + 3 = A y(2y - 2)...

Fastest way to compute upper-triangular matrix of geometric series (Python)

python,numpy,vectorization,linear-algebra
and thanks in advance for the help. Using Python (mostly numpy), I am trying to compute an upper-triangular matrix where each row "j" is the first j-terms of a geometric series, all rows using the same parameter. For example, if my parameter is B (where abs(B)=<1, i.e. B in [-1,1]),...