I am trying to use compressed sensing for a 2D matrix. I am trying to run the following piece of code -
Nf=800;
N=401;
E=E(Nf,N); %matrix of signal(this only for sampling) real matrix E is 2D matrix with size of Nf and N
% compressive sensing
M=ceil(0.3*N);
psi=fft(eye(N));
phi=randi(M,N);
EE = permute(E,[2 1]);
theta=phi*psi;
for k=1:Nf
y(:,k)=phi*EE(:,k);
end
x0 = theta.'*y;
for p=1:Nf
X_hat(:,p) = l1eq_pd(x0(:,p), theta, [], y(:,p), 1e-5); %l1eq_pd=l1-magic
end
X1_hat=psi*X_hat;
XX_hat=permute(X1_hat,[2 1]);
but while running the code I get the following error.
Error using linsolve
Matrix must be positive definite.
Error in l1eq_pd (line 77)
[w, hcond] = linsolve(A*A', b, opts);
Error in simulation_mono_SAR (line 91)
X_hat(:,p) = l1eq_pd(x0(:,p), theta, [], y(:,p), 1e-5);
Could someone point me, what is the problem? Is it a problem inherent to l1-magic? shall I use another solver?
Related
I'm trying to implement an "LOG blob detector" using python and openCV.
the idea is to create 10-15 levels of LOG filters, apply each of them to my original gray scale image and save the images in an array of size height x width x numOfLevels and then find the local maximums on the 3D array.
The problem is I'm not sure how to save these in an array.
I tried to do the following:
myImage = cv2.imread('butterfly.jpg')
gray_image = cv2.cvtColor(myImage, cv2.COLOR_BGR2GRAY)
sigma = 2
k = 2**(0.25)
std2 = float(sigma**2)
arr = []
for i in range(10):
filt_size = 2*np.ceil(3*sigma)+1
H = log_filt( filt_size, sigma)
H *= sigma**2
dst = cv2.filter2D(gray_image,-1,H)
arr.append(dst)
cv2.imshow('Gray', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
sigma = sigma * k
std2 = float(sigma**2)
plt.imshow(H,interpolation='nearest')
But then if I try to take on the of the images and use cv2.imshow(arr[0]) I get the following error:
TypeError: Required argument 'mat' (pos 2) not found
What am I doing wrong here?
Is there a better way to save these in an array?
Maybe using np.array somehow?
Your error:
cv2.imshow(arr[0]) I get the following error: TypeError: Required argument 'mat' (pos 2) not found
Is because you pass the image (arr[0]) as first parameter, but you should pass it as second:
cv2.imshow('WindowName', arr[0])
See the OpenCV 2.4 and 3.0 documentation of imshow:
cv2.imshow(winname, mat)
I'm dealing with a big sparse matrix (10k x 10k variance/covariance matrix, so symmetric and positive definite) in R. I need a fast way to find the Cholesky decomposition of that matrix. I understand that using SparseLU from RcppEigen package can be a solution but I can't figure out how it works.
In the next script, I put the example of my function in R. It loads a SparseMatrix SS, of which I need the Cholesky decomposition. I'd like to have the same output as chol(SS), with typical R function chol.
cholScript<-'using Eigen::Map;
using Eigen::SparseMatrix;
const SparseMatrix<double> Sigma(as<SparseMatrix<double> >(Sigma));
using namespace Rcpp;
// compute C, the Cholesky decomposition of Sigma
return wrap(C);'
cxxfunction(signature(Sigma = "dgCMatrix"), cholScript, plugin = "RcppEigen")
Thank you very much.
I think that this can be a solution:
CholeskyCppSparse<-'using Eigen::Map;
using Eigen::SparseMatrix;
using Eigen::LLT;
const SparseMatrix<double> SS(as<SparseMatrix<double> >(Sigma));
typedef Eigen::SimplicialLLT<SparseMatrix<double> > SpChol;
const SpChol Ch(SS);
return wrap(Ch.matrixL());'
CholSparse <- cxxfunction(signature(Sigma = "dgCMatrix"), CholeskyCppSparse, plugin = "RcppEigen")
Change matrixL for matrixU if you want the upper triangular part of the decomposition (as for chol() function in R).
I'm stuck with computing the integral at each point of an array. The idea is first to create a function ("Integrand"). Then, create a second function ("MyConvolve") that computes the necessary integral.
Here's what I did up to now:
Integrand = function(s,x)
{ 1/4*(abs(x-s)<=1)*(abs(s)<=1) }
MyConvolve = function(func,data)
{ return( integrate(func, lower=-Inf, upper=Inf, data) ) }
Now, running the code with some array, I get an error message:
SomeMatrix = replicate(10, rnorm(10))
MyConvolve(Integrand, SomeMatrix)
Which ends up with the following error message:
Error in integrate(func, lower = -Inf, upper = Inf, data) :
evaluation of function gave a result of wrong length
I already tried vectorizing the function, but still ended up with error messages.
Thank you very much for your help!
I am not sure I understand what you are trying to compute,
but if you want to evaluate MyConvolve(Integrand,s),
where s takes all the values in SomeMatrix,
then apply is sufficient.
sapply( SomeMatrix, function(s) MyConvolve( Integrand, s )$value )
However, the dimensions of the matrix are lost.
You can recover them as follows:
result <- SomeMatrix
result[] <- sapply( SomeMatrix, function(s) MyConvolve( Integrand, s )$value )
I've been coding some simulations using inline/RcppArmadillo and stumbled upon a problem with RNGScope. Is this a bug or am I doing something really dumb?? I've emptied the function out to make it readable (see below).
Cheers,
Ed
library(inline)
code_cpp <- '
using namespace arma;
// From R
uvec x0 = as<uvec>(x0_r);
vec time_vec = as<vec>(time_vec_r);
// Declare variables
umat simulation_data=zeros<umat>(x0.n_elem, time_vec.n_elem);
RNGScope scope;
return wrap(simulation_data);
'
gillespie_sim <- cxxfunction(body = code_cpp,
sig = signature(x0_r = "integer", time_vec_r= "numeric"),
plugin = "RcppArmadillo")
x0 <- c(1,0,0,0,0,0)
time_vec <- 1:800
set.seed(23)
for(i in 1:100000) out2 <- gillespie_sim(x0_r=x0,time_vec_r=time_vec)
The error I get
R(43305) malloc: * error for object 0x108c30a00: incorrect checksum for freed object - object was probably modified after being freed.
* set a breakpoint in malloc_error_break to debug
Abort trap: 6
Hm, I see two issues:
a) You use umat, but we have no unsigned int in R, so this will get lots of very inefficient copies. I changed it to mat, but imat should work too.
b) You loop a lot with for(i in 1:100000). We have seen similar issue with "gazillions" of object creations. We are not sure where the bug is.
With a smaller N it does not seem to happen (as often). We'll take a look and see if RNGScope has anything to do with it -- but that is a very simple object.
Thanks for the bug report. Consider using rcpp-devel next time, please.
Edit: Also note that when using Rcpp vectors, the error does not seem to manifest itself. So you could use the two-step method of first initializing Rcpp objects and then initializing Armadillo objects from it -- the fastLm.r file in the package has an example of that.
suppressMessages(library(Rcpp))
suppressMessages(library(inline))
useRcpp <- function() {
code_cpp <- '
// From R
NumericVector x0(x0_r);
NumericVector time_vec(time_vec_r);
// Declare variables
NumericMatrix simulation_data(x0.size(), time_vec.size());
RNGScope scope;
return simulation_data;
'
cxxfunction(body = code_cpp,
sig = signature(x0_r = "integer", time_vec_r= "numeric"),
plugin = "Rcpp")
}
gillespie_sim <- useRcpp()
x0 <- c(1,0,0,0,0,0)
time_vec <- 1:800
set.seed(23)
for(i in 1:100000) out2 <- gillespie_sim(x0_r=x0,time_vec_r=time_vec)
cat("Done\n")
I have two variables, the first is 1D flow vector containing 230 data and the second is 2D temperature matrix (230*44219).
I am trying to find the correlation matrix between each flow value and corresponding 44219 temperature. This is my code below.
Houlgrave_flow_1981_2000 = window(Houlgrave_flow_average, start = as.Date("1981-11-15"),end = as.Date("2000-12-15"))
> str(Houlgrave_flow_1981_2000)
‘zoo’ series from 1981-11-15 to 2000-12-15
Data: num [1:230] 0.085689 0.021437 0.000705 0 0.006969 ...
Index: Date[1:230], format: "1981-11-15" "1981-12-15" "1982-01-15" "1982-02-15" ...
Hulgrave_SST_1981_2000=X_sst[1:230,]
> str(Hulgrave_SST_1981_2000)
num [1:230, 1:44219] -0.0733 0.432 0.2783 -0.1989 0.1028 ...
sf_Houlgrave_SF_SST = NULL
sst_Houlgrave_SF_SST = NULL
cor_Houlgrave_SF_SST = NULL
for (i in 1:230) {
for(j in 1:44219){
sf_Houlgrave_SF_SST[i] = Houlgrave_flow_1981_2000[i]
sst_Houlgrave_SF_SST[i,j] = Hulgrave_SST_1981_2000[i,j]
cor_Houlgrave_SF_SST[i,j] = cor(sf_Houlgrave_SF_SST[i],Hulgrave_SST_1981_2000[i,j])
}
}
The error message always says:
Error in sst_Houlgrave_SF_SST[i, j] = Hulgrave_SST_1981_2000[i, j] :
incorrect number of subscripts on matrix
Thank you for your help.
try this:
# prepare empty matrix of correct size
cor_Houlgrave_SF_SST <- matrix(nrow=dim(Hulgrave_SST_1981_2000)[1],
ncol=dim(Hulgrave_SST_1981_2000)[2])
# Good practice to not specify "230" or "44219" directly, instead
for (i in 1:dim(Hulgrave_SST_1981_2000)[1]) {
for(j in 1:dim(Hulgrave_SST_1981_2000)[2]){
cor_Houlgrave_SF_SST[i,j] <- cor(sf_Houlgrave_SF_SST[i],Hulgrave_SST_1981_2000[i,j])
}
}
The two redefinitions inside your loop were superfluous I believe. The main problem with your code was not defining the matrix - i.e. the cor variable did not have 2 dimensions, hence the error.
It is apparently also good practice to define empty matrices for results in for-loops by explicitly giving them correct dimensions in advance - is meant to make the code more efficient.