Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 months ago.
Improve this question
I want to solve an Algebraic equation in matrix form:
And the equation looks like this:
I need to solve it with respect to k'(kp in matlab code), and integer p from -∞ to ∞, k∈(-1,1).
So, actually, the equation is AX=0, and I need to solve det(A)=0 with respect to k' when k∈(-1,1). Actually I only need to solve it with p run over -3,-2,...,2,3 when k∈(-1,1) to get values at each k.
This is easy in Matlab by running:
...
syms kp;
A11 = kp^2 + ep0 - ( k - 6 )^2;
A22 = kp^2 + ep0 - ( k - 4 )^2;
A33 = kp^2 + ep0 - ( k - 2 )^2;
A44 = kp^2 + ep0 - ( k + 0 )^2;
A55 = kp^2 + ep0 - ( k + 2 )^2;
A66 = kp^2 + ep0 - ( k + 4 )^2;
A77 = kp^2 + ep0 - ( k + 6 )^2;
A1 = [ A11 ep_1 ep_2 ep_3 ep_4 ep_5 ep_6 ];
A2 = [ ep1 A22 ep_1 ep_2 ep_3 ep_4 ep_5 ];
A3 = [ ep2 ep1 A33 ep_1 ep_2 ep_3 ep_4 ];
A4 = [ ep3 ep2 ep1 A44 ep_1 ep_2 ep_3 ];
A5 = [ ep4 ep3 ep2 ep1 A55 ep_1 ep_2 ];
A6 = [ ep5 ep4 ep3 ep2 ep1 A66 ep_1 ];
A7 = [ ep6 ep5 ep4 ep3 ep2 ep1 A77 ];
A = [ A1; A2; A3; A4; A5; A6; A7 ];
kk = double(solve(det(A)));
kk = kk(1:length(kk)/2);
kk = abs(kk)/sqrt(1+n0);
kpcal = [ kpcal ; kk ];
...
But it may cost a few hours, so I want to try it with Julia, How can I do that?
Any suggestion will be helpful!
I don't know specific functions that can do the same thing in Julia to "solve" the function in Matlab.
Related
This question already has answers here:
Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?
(1 answer)
Why does mulss take only 3 cycles on Haswell, different from Agner's instruction tables? (Unrolling FP loops with multiple accumulators)
(1 answer)
How to optimize these loops (with compiler optimization disabled)?
(3 answers)
When, if ever, is loop unrolling still useful?
(9 answers)
how will unrolling affect the cycles per element count CPE
(1 answer)
Closed 1 year ago.
So I have the results for some experiments and I need to write about the efficiency of pipelining.
I don't have the source code but I do have the time it took for a 4 layer pipeline and an 8 layer pipeline to sum an array of 100,000,000 doubles.
The sum was performed the following way.
For the 4-layer pipeline
d0 = 0.0; d1 = 0.0; d2 = 0.0; d3 = 0.0;
for (int i = 0; i < N; i = i + 4) {
d0 = d0 + a[i + 0];
d1 = d1 + a[i + 1];
d2 = d2 + a[i + 2];
d3 = d3 + a[i + 3];
}
c = d0 + d1 + d2 + d3;
for the 8 layer pipeline
d0 = 0.0; d1 = 0.0; d2 = 0.0; d3 = 0.0;
d4 = 0.0; d5 = 0.0; d6 = 0.0; d7 = 0.0;
for (int i = 0; i < N; i = i + 8) {
d0 = d0 + a[i + 0];
d1 = d1 + a[i + 1];
d2 = d2 + a[i + 2];
d3 = d3 + a[i + 3];
d4 = d4 + a[i + 4];
d5 = d5 + a[i + 5];
d6 = d6 + a[i + 6];
d7 = d7 + a[i + 7];
}
c = d0 + d1 + d2 + d3 + d4 + d5 + d6 + d7;
The results I have show the following time values for No pipeline , 2 layer pipeline , 4 layer pipeline and 8 layer pipeline. The code for the no pipeline and 2 -layer pipeline is similar to the ones I showed above. The results are averaged over 10 runs and are as follows. The experiment was run in an Intel Core i7-9750H Processor.
No Pipeline : 0.106 secs
2-Layer-Pipeline: 0.064 secs
4-Layer-Pipeline: 0.046 secs
8-Layer-Pipeline: 0.048 secs
It is evident that from no pipeline to 4 pipeline the effiency gets better but I'm trying to think of ways as to why the efficiency actually got worst from the 4-layer pipeline to the 8 layer-pipeline. Considering that the sum is done by different registers then there shouldn't be any type of dependency hazard affecting the values. One Idea that I had is that maybe there aren't enough ALUs to process more than 4 floating point numbers at one time and this causes stalls but then wouldn't it at least perform better than the 4 stage pipeline. I have plotted the processes in excel to try to find where the stalls/bubbles are happening but I can't see any.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I have an equation which I want to simplify in order to retrieve the solution. But the equation contains absolute in it. How can it be more simplified?
d = abs(X²-vs²)/2*a1 + abs(vf²-X²)/2*a2
abs is the absolute of the value inside the parenthesis
X is the unknown and we know d, vs, a1, vf and a2
So in the end the equation would be X = ....
A solution for this would be to solve the 4 following equations:
d = (X²-vs²)/2*a1 + (vf²-X²)/2*a2
d = (vs²-X²)/2*a1 + (vf²-X²)/2*a2
d = (X²-vs²)/2*a1 + (X²-vf²)/2*a2
d = (vs²-X²)/2*a1 + (X²-vf²)/2*a2
And then see which solutions are compatible with the initial equation.
The main idea is to rewrite the one abs(A) ... = 0 equation into 2 with conditions:
abs(A) ... = 0
is equal to
A ... = 0 when A >= 0
-A ... = 0 when A < 0
In your case we can turn then initial equation into three . Let's suppose that
vs**2 < vf**2
so we have 3 cases:
X <= vs so abs(X**2 - vs**2) = -(X**2 - vs**2) ; (vf**2 - X**2) = vf**2 - X**2
vs < X <= vf so abs(X**2 - vs**2) = X**2 - vs**2 ; (vf**2 - X**2) = vf**2 - X**2
vf < X so abs(X**2 - vs**2) = X**2 - vs**2 ; (vf**2 - X**2) = -(vf**2 - X**2)
Ans we have three easy to solve equations with conditions on X:
d = -(X**2 - vs**2) / 2 / a1 + (vf**2 - X**2)/2 * a2 when X <= vs
d = (X**2 - vs**2) / 2 / a1 + (vf**2 - X**2)/2 * a2 when vs < X <= vf
d = (X**2 - vs**2) / 2 / a1 - (vf**2 - X**2)/2 * a2 when vf < X
Solve all of them, but take X if and only if it met the condition. In case that vs**2 > vf**2 you have 3 a bit different conditions:
X <= vf
vf < X <= vs
vs < X
and equations will be
d = -(X**2 - vs**2) / 2 / a1 + (vf**2 - X**2)/2 * a2 when X <= vf
d = -(X**2 - vs**2) / 2 / a1 - (vf**2 - X**2)/2 * a2 when vs < X <= vf
d = (X**2 - vs**2) / 2 / a1 - (vf**2 - X**2)/2 * a2 when vf < X
By definition:
abs(x) = x if x >= 0, &
-x if x <=0
So turn this equation into 4 equations, for all combinations of each abs term positive or negative, i.e. + +, + -, - +, - -
Solve each of the four equations, and discard any solutions that don't satisfy the sign assumption about the abs term of that particular equation after you plug the answer back into the abs term. Any solutions remaining that do satisfy the abs sign assumption are the actual answers.
I have a list L = [a13 == a10, a14 == a11, a15 == a12, a16 == a7, a17 == a8, a18 == a9]
I then have a running through a loop giving it these values
a = 1
a = 2*a15*a16 + 2*a13*a17 + 2*a13*a18 +1849
etc
I have
print(a)
a.subs(L)
print(a)
and it indicates no change, but I would of thought/ expected substitution to of taken place. Maybe I am being idiot, but please tell me where.
Thanks.
Edit: Example code
I will write out some of my code + outputs:
print L
while k <= i[0].degree(t):
a = i[0].coefficient({t:k})
print a
b = a.subs(L)
print b
Don't understand why there is an extra box, but hopefully this makes sense.
An example of Outputs:
[a13 == a10, a13 == a11, a15 == a12, a16 == a7, a17 == a8, a18 == a9]
1
1
1
1
2*a15*16 + 2*a14*a17+2*a13*a13 + 1849
2*a15*16 + 2*a14*a17+2*a13*a13 + 1849
Hope this helps
I think that what you are missing is that a.subs(input) is not intended to modify a - presumably so that one may do it many times. Why not try this:
b = a.subs(L)
print b
I am trying to curve fit 5 points in C. I have used this code from a previous post (Can sombody simplify this equation for me?) to do 4 points, but now I need to add another point.
// Input data: arrays x[] and y[]
// x[1],x[2],x[3],x[4] - X values
// y[1],y[2],y[3],y[4] - Y values
// Calculations
A = 0
B = 0
C = 0
D = 0
S1 = x[1] + x[2] + x[3] + x[4]
S2 = x[1]*x[2] + x[1]*x[3] + x[1]*x[4] + x[2]*x[3] + x[2]*x[4] + x[3]*x[4]
S3 = x[1]*x[2]*x[3] + x[1]*x[2]*x[4] + x[1]*x[3]*x[4] + x[2]*x[3]*x[4]
for i = 1 to 4 loop
C0 = y[i]/(((4*x[i]-3*S1)*x[i]+2*S2)*x[i]-S3)
C1 = C0*(S1 - x[i])
C2 = S2*C0 - C1*x[i]
C3 = S3*C0 - C2*x[i]
A = A + C0
B = B - C1
C = C + C2
D = D - C3
end-loop
// Result: A, B, C, D
I have been trying to covert this to a 5 point curve fit, but am having trouble figuring out what goes inside the loop:
// Input data: arrays x[] and y[]
// x[1],x[2],x[3],x[4],x[5] - X values
// y[1],y[2],y[3],y[4],y[5] - Y values
// Calculations
A = 0
B = 0
C = 0
D = 0
E = 0
S1 = x[1] + x[2] + x[3] + x[4]
S2 = x[1]*x[2] + x[1]*x[3] + x[1]*x[4] + x[2]*x[3] + x[2]*x[4] + x[3]*x[4]
S3 = x[1]*x[2]*x[3] + x[1]*x[2]*x[4] + x[1]*x[3]*x[4] + x[2]*x[3]*x[4]
S4 = x[1]*x[2]*x[3]*x[4] + x[1]*x[2]*x[3]*[5] + x[1]*x[2]*x[4]*[5] + x[1]*x[3]*x[4]*[5] + x[2]*x[3]*x[4]*[5]
for i = 1 to 4 loop
C0 = ??
C1 = ??
C2 = ??
C3 = ??
C4 = ??
A = A + C0
B = B - C1
C = C + C2
D = D - C3
E = E + C4
end-loop
// Result: A, B, C, D, E
any help in filling out the C0...C4 would be appreciated. I know this has to do with the matrices but I have not been able to figure it out. examples with pseudo code or real code would be most helpful.
thanks
I refuse to miss this opportunity to generalize. :)
Instead, we're going to learn a little bit about Lagrange polynomials and the Newton Divided Difference Method of their computation.
Lagrange Polynomials
Given n+1 data points, the interpolating polynomial is
where l_j(i) is
.
What this means is that we can find the polynomial approximating the n+1 points, regardless of spacing, etc, by just summing these polynomials. However, this is a bit of a pain and I wouldn't want to do it in C. Let's take a look at Newton Polynomials.
Newton Polynomials
Same start, given n+1 data points, the approximating polynomial is going to be
where each n(x) is
with a coefficient of
, being the divided difference.
The final form end's up looking like
.
As you can see, the formula is pretty easy given the divided difference values. You just do each new divided difference and multiply by each point so far. It should be noted that you'll end up with a polynomial of degree n from n+1 points.
Divided Difference
All that's left is to define the divided difference which is really best explained by these two pictures:
and
.
With this information, a C implementation should be reasonable to do. I hope this helps and I hope you learned something! :)
If the x values are equally spaced with x2-x1=h, x3-x2=h, x4-x3=h and x5-x4=h then
C0 = y1;
C1 = -(25*y1-48*y2+36*y3-16*y4+3*y5)/(12*h);
C2 = (35*y1-104*y2+114*y3-56*y4+11*y5)/(24*h*h);
C3 = -(5*y1-18*y2+24*y3-14*y4+3*y5)/(12*h*h*h);
C4 = (y1-4*y2+6*y3-4*y4+y5)/(24*h*h*h*h);
y(x) = C0+C1*(x-x1)+C2*(x-x1)^2+C3*(x-x1)^3+C4*(x-x1)^4
// where `^` denotes exponentiation (and not XOR).
I have two numbers that are samples of two different quantities (it doesn't really matter what it is). They are both fluctuating with time. I have samples for these values from two different points in time. Call them a0, a1, b0, b1. I can use the differences (a1-a0, b1-b0) the difference and sum of the differences ( (a1-a0)-(b1-b0) ) ( (a1-a0) + (b1-b0) ) )
My questions is how do you determine when both of them are descending in an fashion that doesn't hard code any constants. Let me explain.
I want to detect when both of these quantities have decreased by a certain amount but that amount may change if I change the quantities I'm sampling so I can't hard code a constant.
I'm sorry if this is vague but that's really all the information I have. I was just wondering if this is even solvable.
if ( a1 - a0 < 0)
if( b1 - b0 < 0) {
//... descending
}
or:
if ( a1 - a0 + b1 - b0 < a1 - a0) // b1 - b0 is negative
if( a1 - a0 + b1 - b0 < b1 - b0) { // a1 - a0 is negative
//... descending
}
To add a threshold is simple:
if ( a1 - a0 < -K)
if( b1 - b0 < -K) {
//... descending, more than K
}
or:
if ( a1 - a0 + b1 - b0 < a1 - a0 - K) // b1 - b0 is less than -K
if( a1 - a0 + b1 - b0 < b1 - b0 - K) { // a1 - a0 is less than -K
//... descending more than K
}