People who code: we want your input. Take the Survey

# Tag Info

22

I'm going to disagree with some of the other answers and say that I believe that figuring out how to use LAPACK is important in the field of scientific computing. However, there is a large learning curve to using LAPACK. This is because it is written at a very low level. The disadvantage of that is that it seems very cryptic, and not pleasant to the senses. ...

16

As far as I know, Lapack is the only publicly available implementation of a number of algorithms (nonsymmetric dense eigensolver, pseudo-quadratic time symmetric eigensolver, fast Jacobi SVD). Most libraries that don't rely on BLAS+Lapack tend to support very primitive operations like matrix multiplication, LU factorization, and QR decomposition. Lapack ...

13

When you use ZGELSS to sovle this problem, you're using the truncated singular value decomposition to regularize this extremely ill-conditioned problem. it's important to understand that this library routine is not attempting to find a least squares solution to $Ax=b$, but rather it is attempting to balance finding a solution that minimizes $\| x \|$ ...

9

This can be interpreted as summing over an index of a tensor when the vector $x$ is reshaped into a box of numbers instead of a list. In particular, if $X$ is the $d\text{-by-}d$ folded version of $x$, then the operation you are doing is, \begin{align} Dx &= \mathrm{vec}\left((I \otimes \mathbf{1})\mathrm{vec}(X)\right) \\ &= \mathrm{vec}(\mathbf{1}^...

9

OpenBlas is quite fast, so you can link it to LAPACK. Have you tried precompiled version of LAPACK/BLAS from your CPU vendor? For example AMD ACML (free) or Intel MKL (free on linux for non-commercial and non-academic use)? You simply need to unpack and run install file. In my opinion the only advantage of using ATLAS is then when you use some unusual CPU. ...

9

Jed Brown has already pointed this out in the comments to the question, but there is really not very much you can do in usual double precision if your condition number is large: in most cases, you will likely not get a single digit of accuracy in your solution and, worse, you can't even tell because you can't accurately evaluate the residual corresponding to ...

9

There's no reason to append a row of 1's. You should just perform a rank-revealing QR factorization (like with routine SGEQP3) on $A^T$, and the last column of $Q$ should be in the nullspace. This has the added advantage that the relative magnitude of the last element on the diagonal of $R$ gives you some idea of how singular the solution is. Even better ...

8

There is a neat trick I have recently learned from this paper. You start doing rank-revealing QR, and stop after the first $k$ Householder reflections, when you have a matrix of the form $$\begin{bmatrix} R_1 & R_{12}\\ 0 & R_{22} \end{bmatrix},$$ with $R_1$ triangular of size $k\times k$, and $R_{22}$ typically not triangular (since we stopped ...

8

The matrix B (M in the documentation) needs to positive definite according to the documentation: "If sigma is None, M is positive definite", this is in addition to the first requirement "M must represent a real, symmetric matrix if A is real" which your B follows. The eigenvalues of your current matrix B are -1, 1 and 6. So matrix B is ...

7

The repository package is not safe to use with threading due to the way it was compiled. I reported the bug on the Lapack forum, but it will take a long time for workarounds or solutions to trickle down into the repository. If you compile it yourself, be sure to add the "-frecursive" to gfortran.

7

The simplest/fastest way to solve ill-conditioned problems is to increase precision of computations (by brute force). Another (yet not always possible) way is to re-formulate your problem. You might need to use quadruple precision (34 decimal digits). Even though 20 digits will be lost in a course (because of condition number) you will still get 14 correct ...

7

Took me quite a while to figure this out and as usual it becomes obvious after you find the culprit. After checking the problematic cases reported in David S. Watkins. A case where balancing is harmful. Electron. Trans. Numer. Anal, 23:1–4, 2006 and also the discussion here (both being cited in arXiv:1401.5766v1), it turns out that matlab uses the ...

7

The cost of the multiplication is almost insignificant compared to the cost of loading the data from memory (and writing it back). If you're worried about performance, you should be thinking about data locality. Perform more flops with each of your $x_i$ and $z_i$ values (if that's possible) when you load them from memory and before proceeding on. Setting ...

7

I usually resist telling people what I think they should do rather than answering their question but in this case I'm going to make an exception. Lapack is written in FORTRAN and the API is very FORTRAN-like. There is a C API to Lapack that makes the interface slightly less painful but it will never be a pleasant experience to use Lapack from C++. ...

7

Here's another answer in the same vein as the above. You should look into the Armadillo C++ linear algebra library. Pros: The function syntax is high-level (similar to that of MATLAB). So no DGESV mumbo-jumbo, just X = solve( A, B ) (although there is a reason behind those oddly-looking LAPACK function names...). Implements various matrix decompositions ...

7

The problem, of course, is that computing the true rank (e.g., via a QR decomposition) is not really any cheaper than computing a low-rank representation of the matrix. The best you can probably do is to use a randomized algorithm to find low-rank approximations. These can, at least in theory, be significantly faster than working on the entire matrix ...

7

It's the blocked variant of Householder-QR that is driving this design. If you look in Golub and Van Loan's book (Ch 5.2 or so) they talk about how k-iterations of the algorithm can be blocked together by accumulating the individual reflectors into a rank-k reflector of the form $\mathbf I + \mathbf W \mathbf Y^{\mathrm T}$, where both $\mathbf W$ and $\... 7 I think you are overestimating the overhead of computing L. There are zero extra operations needed; the only additional cost is writing to RAM some numbers that you have already computed anyway. The algorithms commonly used (in Lapack, for instance) to compute U also compute L along the way, and you'd save 0 flops by omitting it. For instance, if you think ... 7 When one says an algorithm is of order$O(n)$, that may mean that the complexity is given by:$c + b*n$. With every new element you add you increase in runtime (effectively). What mathematically minded people often forget is that these statements do not include how large the constants are. That of course carries over to$O(n²)$and such. I can not answer ... 6 In my experience, the best way to use blas/lapack on recent versions of ubuntu is to use the packaged openblas. For what it's worth, I mostly use blas/lapack through python numpy/scipy, and using openblas speeds up some of the linear algebra by like 200x vs. the default. I've tried using custom ATLAS, but it was a huge pain and didn't give much if any ... 6 I second the idea of using Eigen, which is pretty efficient, but also very simple to include. If you need a lot more performance, you could try to use PETSc or Trilinos. They are very powerful libraries to store and solve sparse systems, they allow for a large number of iterative or direct solvers and are compatible with MPI for added performance. However, ... 6 Seems that you have a duplicate eigenvalue. Thus, you have two eigenpairs$(\lambda_1, x_1)$and$(\lambda_2, x_2)$where$\lambda_1 = \lambda_2$. Denote$\lambda = \lambda_1 = \lambda_2$. Let$\alpha$and$\beta$be arbitrary complex numbers. Then $$A (\alpha x_1 + \beta x_2) = \alpha A x_1 + \beta A x_2 = \alpha \lambda x_1 + \beta \lambda x_2 = \lambda (\... 6 The trick is trying to find out why that matrix has real eigenvalues in the first place. Usually it is because a suitable set of conjugations turns it into a symmetric matrix, and then you can reduce to a symmetric computation. Multiplying and dividing by (AC)^{-1} you can rewrite$$ D_1 = C^{-1}BAC (C^{-1}BAC+I)^{-1},$$so your computation is equivalent ... 5 If you are trying to calculate all 50k eigenvalues of these matrices, that is going to take you a "quite good amount of time" no matter which algorithm you choose. I'm doubtful you can do substantially better than LAPACK. Lanczos (or generally Arnoldi) algorithms are particularly good at calculating a hand full of eigenvalues at either end of the spectrum (... 5 Yes, you want to call the BLAS routine DGEMM. The place to start for how to call it from C is to look at the documentation for DGEMM, which you can find online. Then you want to understand how to call FORTRAN routines from C (DGEMM, like all the standard BLAS routines has a FORTRAN calling convention). For example, this document https://computing.llnl.gov/... 5 The LAPACK routine zgesv first computes the LU factorization, and then solves the system making use of the factorization. It is a simple driver for calling the two routines zgetrf (compute the LU factorization) and zgetrs (solve the system). The LU factorization is computed using partial pivoting and row interchanges. Then, the system is solved using simple ... 5 tl;dr Yes. But your question doesn't make it clear that you understand what LAPACK is about. LAPACK is both a software as well as an interface. That is, the operations that LAPACK defines are standard enough that they can be replaced by other software, such as ATLAS, MKL, and so on. Another way of looking at this is that any software that does linear algebra ... 5 The other answers already tell you what went wrong, but I will add a terminology note: the term for what is happening is that the pencil$A - \lambda B$is a singular matrix pencil, i.e.,$\det (A - \lambda B)\$ is identically equal to zero. So there are no generalized eigenvalues (or, at least, they cannot be defined as usual as the roots of the generalized ...

4

Eigen's LDLT class actually performs a U^T.D.U factorization if the input matrix is column-major with the symmetric entries stored in the upper triangular part: MatrixXd A; // fill at least the upper triangular part of A LDLT<MatrixXd,Upper> udu(A); // Only the upper part of A is read to form U udu.matrixU(); // returns an expression of the triangular ...

4

dgeev is meant for smallish matrices where you need all eigenvalues. It may be that Mathematica is using a smarter but completely different method, such as Krylov subspace methods to generate the eigenvalues for your matrices.

Only top voted, non community-wiki answers of a minimum length are eligible