Why does scipy.signal.windows.gaussian produce different numbers than Matlab and Octave's gausswin? - scipy

For example we can use a length 16 window with 3 as the deviation:
scipy.signal.windows.gaussian(16,3)
#returns :
array([0.04393693, 0.09563444, 0.18627046, 0.32465247, 0.50633562,
0.70664828, 0.8824969 , 0.98620712, 0.98620712, 0.8824969 ,
0.70664828, 0.50633562, 0.32465247, 0.18627046, 0.09563444,
0.04393693])
Where as octave produces :
gausswin(16,3)
#returns :
0.011109
0.034047
0.088922
0.197899
0.375311
0.606531
0.835270
0.980199
0.980199
0.835270
0.606531
0.375311
0.197899
0.088922
0.034047
0.011109
Matlab produces similar results as Octave. I don't really understand why the numbers would be different on purpose? Is it a bug?

The function gaussian(M, std) from scipy.signal.windows and the Octave function gausswin(m, a) from the Octave signal package do not treat the second parameter in the same way. To translate the Octave parameters m and a to the scipy parameter, you can use
std = (m - 1)/(2*a)
For example, here's how you can get the same values that you produced in Octave with gausswin(16, 3):
In [74]: from scipy.signal.windows import gaussian
In [75]: m = 16
In [76]: a = 3
In [77]: gaussian(m, (m - 1)/(2*a))
Out[77]:
array([0.011109 , 0.03404745, 0.08892162, 0.1978987 , 0.3753111 ,
0.60653066, 0.83527021, 0.98019867, 0.98019867, 0.83527021,
0.60653066, 0.3753111 , 0.1978987 , 0.08892162, 0.03404745,
0.011109 ])

Related

scipy dblquad providing the wrong result in simple double integral

I am trying to calculate a straightforward doble definite integral in Python: function Max(0, (4-12x) + (6-12y)) in the square [0,1] x [0,1].
We can do it with Mathematica and get the exact result:
Integrate[Max[0, 4-12*u1 + 6-12*u2], {u1, 0, 1}, {u2, 0,1}] = 125/108.
With a simple Monte Carlo simulation I can confirm this result. However, using scipy.integrate.dblquad I am getting a value of 0.0005772072907971, with error 0.0000000000031299
from scipy.integrate import dblquad
def integ(u1, u2):
return max(0, (4 - 12*u1) + (6 - 12*u2))
sol_int, err = dblquad(integ, 0, 1, lambda _:0, lambda _:1, epsabs=1E-12, epsrel=1E-12)
print("dblquad: %0.16f. Error: %0.16f" % (sol_int, err) )
Agreed that the function is not derivable, but it is continuous, I see no reason for this particular integral to be problematic.
I thought maybe dblquad has an 'options' argument where I can try different numerical methods, but I found nothing like that.
So, what am I doing wrong?
try different numerical methods
That's what I would suggest, given the trouble that iterated quad has on Windows. After changing it to an explicit two-step process, you can replace one of quad with another method, romberg seems the best alternative to me.
from scipy.integrate import quad, romberg
def integ(u1, u2):
return max(0, (4 - 12*u1) + (6 - 12*u2))
sol_int = romberg(lambda u1: quad(lambda u2: integ(u1, u2), 0, 1)[0], 0, 1)
print("romberg-quad: %0.16f " % sol_int)
This prints 1.1574073959987758 on my computer, and hopefully you will get the same.

Matlab incorrect sum value

In Matlab, I have a vector res of size 1*10.
Also, I have this code:
disp("Matrix:");
disp(res);
disp("Size:");
disp(size(res));
disp("Sum of elements:");
disp(sum(res));
Which outputs:
Matrix:
1.0e+05 *
1.9183 1.6247 1.5875 1.6979 1.3663 1.6511 1.5198 1.3014 1.2067 1.3661
Size:
1 10
Sum of elements:
1.5240e+06
1. What is 1.0e+05 *?
2. How can the sum of 1.9183 1.6247 1.5875 1.6979 1.3663 1.6511 1.5198 1.3014 1.2067 1.3661 be 1.5240e+06?
What I did so far:
I tried to see if there is a name conflict with whos ('res') but no other variable with the same name is in context.
I also tried to eliminate other sum name in context with clear sum but still I get the same result.
The Following notation:
Matrix:
1.0e+05 *
1.9183 1.6247 1.5875 1.6979 1.3663 1.6511 1.5198 1.3014 1.2067
means:
Matrix:
191830 162470 158750 169790 136630 165110 151980 130140 120670
it is a scientific notation to show the result in a standard format. Therefore, the problem of summing is solved by this description.

Error with matrix assignment

Good evening to all,
I would like to affect values of a matrix B to others differents matrix C.
Then i try this code:
B=imread("deformation_somb.jpg");
xc=pixel_x1;
yc=pixel_y1;
% where [nr,nc,channels]= size([B])
% pixel_x1=(nc/2) & pixel_y1=(nr/2);
j=nr-yc;
i=xc;
i_c=1;
j_c=1;
C_horizontale_droite=[];
C_horizontale_gauche=[];
C_verticale_haut=[];
C_verticale_bas=[];
i_nouveau_h=i;
i_nouveau_b=i;
j_nouveau_d=j;
j_nouveau_g=j;
while(i_nouveau_h!=nr-1 || i_nouveau_b!=0 || j_nouveau_g!=0 || j_nouveau_d!=nc-1)
C_horizontale_gauche(1,j_c,:)=B(i,j_nouveau_g,:);
C_horizontale_droite(1,j_c,:)=B(i,j_nouveau_d,:);
C_verticale_haut(1,j_c,:)=B(i_nouveau_h,j,:);
C_verticale_bas(1,j_c,:)=B(i_nouveau_b,j,:);
i_nouveau_h = i_nouveau_h+1;
i_nouveau_b = i_nouveau_b-1;
j_nouveau_d = j_nouveau_d+1;
j_nouveau_g = j_nouveau_g-1;
j_c=j_c+1;
endwhile
distance_pixel_gauche_centre = find(C_horizontale_gauche=C_horizontale_droite!=0)
distance_pixel_droite_centre = find(C_horizontale_droite=C_horizontale_gauche!=0)
distance_pixel_haut_centre= find(C_verticale_haut=C_verticale_bas!=0)
distance_pixel_bas_centre= find(C_verticale_bas=C_verticale_haut!=0)
But for the first affect of matrix :
C_horizontale_gauche(1,j_c,:)=B(i,j_nouveau_g,:);
I have an error message :
error : LG : Subscript indices must be either positive integers less than 2^31 or logicals.
Then i verify indices they are positive. I give you an example :
For an image 700*700 RGB, nr=nc=700.
Then j=700-350=350, i =350
So, j_nouveau_g=350.
Thus, the first expression should sent to Octave this one:
`C_horizontale_gauche(1,1,:)=B(350,350,:)`
... Well.. Why octave send me an error?
Thx for your answers.
Best Regards
Julien

How to save parts of linprog solutions

I am solving a serie of linear programing problems using linprog by indexing each problem in a for-loop:
from scipy.optimize import linprog
for i in range(1,N):
sol[i] = linprog(coa3[N][0], A_ub = coa4[i], b_ub = chvneg[i], options= {"disp": True})
I would like to save in a list (still indexed over i) the function minimization result and the array displaying the values of the variables. I guess I need to add in the for-loop something like minfval[i] = ??? and xval[i] = ???, but actually I don't know how to extract these values from the results provided by linprog. Any suggestions? Thanks a lot.
Consider reading the docs as it's pretty clearly explained what exactly linprog returns.
The good thing is, that you are actually storing these values with your code already because you are storing the whole scipy.optimize.OptimizeResult.
It's now just a matter of accessing it:
from scipy.optimize import linprog
for i in range(1,N):
sol[i] = linprog(coa3[N][0], A_ub = coa4[i], b_ub = chvneg[i], options= {"disp": True})
# iterate over solution-vectors
for i in range(1,N):
print(sol[i].x)
# iterate over objectives
for i in range(1,N):
print(sol[i].fun)
You should also check out the field success just to be safe!
Extraction of the docs (link above) on what is contained in linprog's result:
x : ndarray
The independent variable vector which optimizes the linear
programming problem.
fun : float
Value of the objective function.
slack : ndarray
The values of the slack variables. Each slack variable corresponds
to an inequality constraint. If the slack is zero, then the
corresponding constraint is active.
success : bool
Returns True if the algorithm succeeded in finding an optimal
solution.
status : int
An integer representing the exit status of the optimization::
0 : Optimization terminated successfully
1 : Iteration limit reached
2 : Problem appears to be infeasible
3 : Problem appears to be unbounded
nit : int
The number of iterations performed.
message : str
A string descriptor of the exit status of the optimization.

Matlab two complex number multiplication

( 0.0037 -22.2145i ) / ( 1.2168e+002 -1.2166e+002i )= 0.0913 - 0.0913i
According to calculators on web, division of these two values must be a different value. But MATLAB calculates different (the result of matlab calculation is given on top). the code part which calculates is like:
disp(((MagPlusOne)*(kiz)));
disp((Magnetic*kizPlusOne));
disp(((MagPlusOne*kiz)/(Magnetic*kizPlusOne)));
Matlab is correct. The issue is that the tool you're comparing Matlab's result to does not parse scientific notation. Entering your equation, the online tool interprets it as
(0.0037- 22.2145i)/( 1.2168*e + 2 - (1.2166*e + 2i))
which is clearly a different number.
If the scientific notation is replace with fixed-decimals
( 0.0037 -22.2145i ) / ( 121.68 - 121.66i ),
the tool produces that desired result 0.0913-0.09123i.