dgemv_ is giving error ** On entry to DGEMV parameter number 6 had an illegal value - matrix

I have an array y
` double* y = (double*) malloc( n*sizeof(double) ).`
I have a matrix XT which is of order n*degreea.I am trying to multiply the matrix and array using dgemv_ as follows:
`transa='T'
alpha=1.0
degreeb=1
beta=0.0
XTy is resultant`
`dgemv_(&transa,&degreea,&n,&alpha,XT,&degreea,y,&degreeb,&beta,XTy,&degreeb);`
But it says ** On entry to DGEMV parameter number 6 had an illegal value
Can someone explain this?

Related

modular exponentiation(python)- memory overflow

below is the code for modular expone
ntiation with two different approaches.
in below code expo1() function is the optimal or correct method
and expo2() function is alternative approach for same
QUESTION: why expo2() function does give incorrect results for larger values of base and exponant, at which line of code memory overflows and why expo1() function does not have this problem.
note : i have also added plot of expo1() and expo2() for base 2 and exponent values upto 300, it can be seen in graph that expo1() and expo2() go hand in hand upto exponent 220 with base 2 and after that expo2() starts giving incorrect answers.
##########################code below########################
mod = 1e9 + 7
# Function to return base^exponent mod m
def expo1(base, exponent):
cp = base
cp1 = exponent
ans = 1
while (exponent != 0):
if ((exponent & 1) == 1):
ans = ans * base
ans = ans % mod
base = base * base
base %= mod
exponent >>= 1
return ans# % mod
# Function to return base^exponent mod m
def expo2(base, exponent):
ans=1
for i in range(exponent):
ans *= base
ans%=mod
return ans
plot of expo1() and expo2() for base 2 and exponent values upto 300.
here i have also added a new plot for camparing above two functions with integer and float type mod(1e9 +7) -> comparison of expo1() and expo2() for int and float mod value , and it seems expo1() is the one which was giving wrong output.
By no means any of the functions should result into "memory overflow".
The second function expo2() will cause a Time limit exceeded (if the environment is enforced to raise an exception when the function does not execute in nearly 1.5-2 seconds) for larger values of the variable exponent. For example, when exponent >= 10^7.
You can observe this on online coding portals like Hackerrank/Codechef/Codeforces.
So the process (expo2()) will be killed because it is taking too much time to execute and SIGTSTP will be returned.
IMPORTANT:
Correct the value of variable mod, i.e., mod = int(1e9 + 7) and check the results.
Both the functions return same values.
Why you got the difference?
The variable mod in your code is a floating point value that must have caused floating point rounding issues. Change it to int as suggested.

Sum of first nth term of series [duplicate]

This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 3 years ago.
I am not understanding why my code isn't looping, for any input I give I get the result of "1.00"
I feel like I am being silly and missing something very obvious.
This series is shown in other places most often as 1/3n+1
But if series_sum(1) = "1.00"
then 3+1 = 4 giving you 1/4 to add to your sum for input of 1 which doesn't make sense
def series_sum(n)
sum = 0
if n == 0
return "0.00"
else
for i in 1..n
sum += 1/(1+(3*(i-1)))
end
end
return "%.2f" % sum.to_s
end
for series_sum(1) should be "1.00"
series_sum(2) should be "1.25"
series_sum(3) should be "1.39"
etc
My code gives "1.00" for any input
Why won't this code run the loop and perform the sum?
When you have expression like z = x/y, in ruby it does specify type for output based on operand provided for division. If x & y both are integer, output calculated is also integer and float value is removed.
So to obtain output as float, you need to have one operand at least as float value which can be found using to_f on variable. Here you need to change only,
- sum += 1/(1+(3*(i-1)))
+ sum += 1.0/(1+(3*(i-1)))

Getting a transition matrix to do a correlation in RStudio

I am trying to isolate just the transition matrix so I can do a correlation of the data. I can't get it figured out how to. I can find the values of the transition matrix from "fit". I can't get any values. I need to run a correlation test of the transition matrix. I also need to be able to plug in any data I need from a timeseries so I can't just use the numbers from fit and do the:
A = matrix( c(0.97487437, 0.02512563, 0.00000000, 0.000,
0.04950495, 0.90099010, 0.04950495, 0.000,
0.00000000, 0.05000000, 0.90000000, 0.050,
0.00000000 ,0.00000000, 0.02500000 ,0.975
), # the data elements
nrow=4, # number of rows
ncol=4, # number of columns
byrow = TRUE) # fill matrix by rows
head(A)
dat=data.frame(x=0:600, y=sin((0:600)/120*2*pi))
plot(dat)
str(dat)
dat$state=NA #gives empty column of NA
#turn timeseries values into states 1,2,3,4
ymin=min(dat$y)
ymax=max(dat$y)
ystep=(ymax-ymin)/4
ymark=(1:3)*ystep+ymin
dat$state=(dat$y>ymark[3])+(dat$y>ymark[2])+(dat$y>ymark[1])+
(dat$y>=ymin) #gives the 4 states
head(dat)
Pfit2=markovchainFit(data=dat2$state)$estimate
Pstderr2=markovchainFit(data=dat2$state)$standardError
Pfittm=markovchainFit(data=dat2$state)$transitionMatrix
dim(Pfit2)
[1] 4
> plot(Pfittm)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

Fmodf returning same value

Here's my code:
x = fmodf((float)rand(), (float)(1/sqsize)) + x1;
The problem is, whenever sqsize is a multiple of square-multiple of 4(e.g 4,16,64), it always returns the same numbers. sqsize is the square root for the multiple (so for 4 it is 2, for 16 it is 4, for 64 it is 8, etc).
x1 is defined as:
x1 = fmodf(value,sqsize)/sqsize;
where value is equal to a number in 0-sqsize^2.
Any ideas why I keep getting the same value? Thanks.
Try (float)(1.0/sqsize) --- the (float) cast you are using does not do anything until 1/sqsize is computed at the moment. Meaning it will probably evaluate to 0 all the time.

Algorithm to express elements of a matrix as a vector

Statement of Problem:
I have an array M with m rows and n columns. The array M is filled with non-zero elements.
I also have a vector t with n elements, and a vector omega
with m elements.
The elements of t correspond to the columns of matrix M.
The elements of omega correspond to the rows of matrix M.
Goal of Algorithm:
Define chi as the multiplication of vector t and omega. I need to obtain a 1D vector a, where each element of a is a function of chi.
Each element of chi is unique (i.e. every element is different).
Using mathematics notation, this can be expressed as a(chi)
Each element of vector a corresponds to an element or elements of M.
Matlab code:
Here is a code snippet showing how the vectors t and omega are generated. The matrix M is pre-existing.
[m,n] = size(M);
t = linspace(0,5,n);
omega = linspace(0,628,m);
Conceptual Diagram:
This appears to be a type of integration (if this is the right word for it) along constant chi.
Reference:
Link to reference
The algorithm is not explicitly stated in the reference. I only wish that this algorithm was described in a manner reminiscent of computer science textbooks!
Looking at Figure 11.5, the matrix M is Figure 11.5(a). The goal is to find an algorithm to convert Figure 11.5(a) into 11.5(b).
It appears that the algorithm is a type of integration (averaging, perhaps?) along constant chi.
It appears to me that reshape is the matlab function you need to use. As noted in the link:
B = reshape(A,siz) returns an n-dimensional array with the same elements as A, but reshaped to siz, a vector representing the dimensions of the reshaped array.
That is, create a vector siz with the number m*n in it, and say A = reshape(P,siz), where P is the product of vectors t and ω; or perhaps say something like A = reshape(t*ω,[m*n]). (I don't have matlab here, or would run a test to see if I have the product the right way around.) Note, the link does not show an example with one number (instead of several) after the matrix parameter to reshape, but I would expect from the description that A = reshape(t*ω,m*n) might also work.
You should add a pseudocode or a link to the algorithm you want to implement. From what I could understood I have developed the following code anyway:
M = [1 2 3 4; 5 6 7 8; 9 10 11 12]' % easy test M matrix
a = reshape(M, prod(size(M)), 1) % convert M to vector 'a' with reshape command
[m,n] = size(M); % Your sample code
t = linspace(0,5,n); % Your sample code
omega = linspace(0,628,m); % Your sample code
for i=1:length(t)
for j=1:length(omega) % Acces a(chi) in the desired order
chi = length(omega)*(i-1)+j;
t(i) % related t value
omega(j) % related omega value
a(chi) % related a(chi) value
end
end
As you can see, I also think that the reshape() function is the solution to your problems. I hope that this code helps,
The basic idea is to use two separate loops. The outer loop is over the chi variable values, whereas the inner loop is over the i variable values. Referring to the above diagram in the original question, the i variable corresponds to the x-axis (time), and the j variable corresponds to the y-axis (frequency). Assuming that the chi, i, and j variables can take on any real number, bilinear interpolation is then used to find an amplitude corresponding to an element in matrix M. The integration is just an averaging over elements of M.
The following code snippet provides an overview of the basic algorithm to express elements of a matrix as a vector using the spectral collapsing from 2D to 1D. I can't find any reference for this, but it is a solution that works for me.
% Amp = amplitude vector corresponding to Figure 11.5(b) in book reference
% M = matrix corresponding to the absolute value of the complex Gabor transform
% matrix in Figure 11.5(a) in book reference
% Nchi = number of chi in chi vector
% prod = product of timestep and frequency step
% dt = time step
% domega = frequency step
% omega_max = maximum angular frequency
% i = time array element along x-axis
% j = frequency array element along y-axis
% current_i = current time array element in loop
% current_j = current frequency array element in loop
% Nchi = number of chi
% Nivar = number of i variables
% ivar = i variable vector
% calculate for chi = 0, which only occurs when
% t = 0 and omega = 0, at i = 1
av0 = mean( M(1,:) );
av1 = mean( M(2:end,1) );
av2 = mean( [av0 av1] );
Amp(1) = av2;
% av_val holds the sum of all values that have been averaged
av_val_sum = 0;
% loop for rest of chi
for ccnt = 2:Nchi % 2:Nchi
av_val_sum = 0; % reset av_val_sum
current_chi = chi( ccnt ); % current value of chi
% loop over i vector
for icnt = 1:Nivar % 1:Nivar
current_i = ivar( icnt );
current_j = (current_chi / (prod * (current_i - 1))) + 1;
current_t = dt * (current_i - 1);
current_omega = domega * (current_j - 1);
% values out of range
if(current_omega > omega_max)
continue;
end
% use bilinear interpolation to find an amplitude
% at current_t and current_omega from matrix M
% f_x_y is the bilinear interpolated amplitude
% Insert bilinear interpolation code here
% add to running sum
av_val_sum = av_val_sum + f_x_y;
end % icnt loop
% compute the average over all i
av = av_val_sum / Nivar;
% assign the average to Amp
Amp(ccnt) = av;
end % ccnt loop

Resources