public Matrix solve(Matrix B) {
    if (B.getRowDimension() != n) {
      throw new IllegalArgumentException("Matrix row dimensions must agree.");
    }
    if (!isspd) {
      throw new RuntimeException("Matrix is not symmetric positive definite.");
    }

    // Copy right hand side.
    double[][] X = B.getArrayCopy();
    int nx = B.getColumnDimension();

    // Solve L*Y = B;
    for (int k = 0; k < n; k++) {
      for (int j = 0; j < nx; j++) {
	for (int i = 0; i < k ; i++) {
	  X[k][j] -= X[i][j]*L[k][i];
	}
	X[k][j] /= L[k][k];
      }
    }

    // Solve L'*X = Y;
    for (int k = n-1; k >= 0; k--) {
      for (int j = 0; j < nx; j++) {
	for (int i = k+1; i < n ; i++) {
	  X[k][j] -= X[i][j]*L[i][k];
	}
	X[k][j] /= L[k][k];
      }
    }

    return new Matrix(X,n,nx);
  }