This stems from beam equations. How do I extract and order constants in matrixes to solve a system of equations rather than doing so by hand?
If I start with the differential equation d4 v / dx4 = q. I integrate 4x, and obtain 4 (more) equations, and thus 4x constants/unknown. For example:
v(x) = q x4 / 24 + c1 x3 / 6 + c2 x2 / 2 + c3 x + c4
If I apply the boundary condition v(5) = 0 this equation becomes
v(5) = q × 54 / 24 + c1 × 53 / 6 + c2 × 52 / 2 + c3 × 5 + c4 = 0, or
c1 × 53 / 6 + c2 × 52 / 2 + c3 × 5 + c4 = -q × 54 / 24
Of course to solve for 4 coefficients the other 3 DEs are used with 3 more boundary conditions. I was wondering if any one knows how I could sort and extract the constants to built up the system of equations rather than doing so by hand.
Related
For example, A polynomial is defined as follows:
f := (x, y) -> 333.75y^6 + x^2(11x^2y^2 - y^6 - 12y^4 - 2) + 5.5y^8 + 1/2*x/y
In maple, I look to evaluate this to 5 significant figures like so:
evalf[5](f(77617,33096))
And obtain a value that is: 1*10^32.
Why is this not to 5 sig fig? Why is this not close to a value of 7.878 * 10^29 as you increase the number of sig fig required?
Thanks!
Don't reduce the working precision that low, especially if you are trying to compute an accurate answer (and then round it for convenience).
More importantly, for compound expressions the floating-point working precision (Digits, or the index of an evalf call) is just that: a specification of working precision and not an accuracy request.
By lowering the working precision so much you are seeing greater roundoff error in the floating-point computation.
restart;
f := (x, y) -> 333.75*y^6
+ x^2*(11*x^2*y^2 - y^6 - 12*y^4 - 2)
+ 5.5*y^8 + 1/2*x/y:
for d from 5 to 15 do
evalf[5](evalf[d](f(77617,33096)));
end do;
32
1 10
31
-3 10
30
1 10
29
8 10
29
7.9 10
29
7.88 10
29
7.878 10
29
7.8784 10
29
7.8785 10
29
7.8785 10
29
7.8785 10
I have been reading some of the LibreOffice code, and there is code that converts from 100mm to twips.
Its basic formula is:
twips = (n>=0) ? (n*72+63) / 127 : (n*72-63) / 127;
Now I know that one twip is 1/20th of a point, and that one inch is 72 points, and 1 inch is 2.54cm, but I cannot work out how the above formula relates to these ratios!
Can anyone shed some light on this?
Putting together what OP provided:
n is the size in 100 mm.
1 inch is 2.54 cm is 25.4 mm.
inchs = n * 100 / 25.4
or inchs = n / (100 * 25.4)
or inchs = n / 2540
1 inch is 72 points.
points = inchs * 72
1 twip is 1/20th point.
twips = points / 20
Now, everything together:
twips = n / 2540 * 72 / 20
or twips = n * 72 / 2540 / 20
or twips = n * 72 / 127
If this is done in int arithmetic there will be truncation instead of mathematical rounding. This can be fixed by adding the half of 127 (127 / 2 = 63) to n * 72:
twips = (n * 72 + 63) / 127
This does not handle negative numbers correctly. For these, the 63 has to be subtracted instead:
twips = n >= 0 ? (n * 72 + 63) / 127) : (n * 72 - 63) / 127;
and here we are.
As already pointed out by Ron, the ?: operator is the ternary if-then-else operator.
An easier to read (but may be less optimized) replacement would be:
if (n >= 0) twips = (n * 72 + 63) / 127);
else twips = (n * 72 - 63) / 127;
This question already has answers here:
What is special about numbers starting with zero?
(4 answers)
Closed 5 years ago.
I recently came across the following when I was testing my code for various value of x.
I will try to illustrate only the issue.
#include <iostream>
int main()
{
int x = 01234;
std:: cout << x ;
return 0;
}
Output:
when x = 1234 , 1234
x = 01234 , 668
x = 001234 , 668
x = 240 , 240
x = 0240 , 160
x = 00240 , 160
For mostly any number starting with 0 I get a different value.
eg: x = 0562 gives 370 and so on.
I tried using various online C++ compilers but all give same output.
I tried to google the issue but couldn't find an appropriate answer.
Looks like you've been hit with an octal literal! Any number literal beginning with just 0 is interpreted in base 8.
01234 = 1 × 8^3 + 2 × 8^2 + 3 × 8^1 + 4 × 8^0
= 1 × 512 + 2 × 64 + 3 × 8 + 4 × 1
= 512 + 128 + 24 + 4
= 668
0240 = 2 × 8^2 + 4 × 8^1 + 0 × 8^0
= 2 × 64 + 4 × 8 + 0 × 1
= 128 + 32
= 160
The number 01234 is in octal (base 8) when you prepend a 0 you define the number as an octal. When you then print it in decimal you get it's decimal equivalent
I have two distance matrices with overlapping variable names.
dfA:
Start A1 A2 A3 A4 … A150
Location
A 12 4 12 2 9
B 5 2 19 4 3
C 1 4 8 7 12
dfB:
A B C
X 4 12 32
Y 1 6 12
Z 2 8,5 11
So from start A1, A2, etc. through ABC there are paths to X, Y and Z
I would like to see what is the shortest path for an item, for example the the combination A1 -> Z. I programmed this by loading csv's with the distance matrices and unstack them. Then with df.itterows() and two for loops loop through the possible combinations and see what the smallest is for the combination A1 -> Z.
But since i have to do this for around 30000 items, it takes way to long.
Anybody know how to do this in a vectorized way?
I added D so that the axis lengths will be different (dfB won't be square matrix) just for my convenience (it works with square matrices too).
import pandas as pd
import numpy as np
df_a = pd.read_csv('dfA.csv', delim_whitespace=True, index_col=0, decimal=",")
df_b = pd.read_csv('dfB.csv', delim_whitespace=True, index_col=0, decimal=",")
mat_a = df_a.values
mat_b = df_b.values
mat_a2 = np.expand_dims(mat_a, axis=2)
mat_b2 = np.expand_dims(mat_b.T, axis=1)
mat_a3 = np.tile(mat_a2, (1, 1, mat_b.shape[0]))
mat_b3 = np.tile(mat_b2, (1, mat_a.shape[1], 1))
tot = mat_a3 + mat_b3
ind = np.argmin(tot, axis=0).T
df_c = pd.DataFrame(df_b.columns.values[ind], columns=df_a.columns, index=df_b.index)
print(df_c)
dfA:
Start_Location A1 A2 A3 A4 A150
A 12 4 12 2 9
B 5 2 19 4 3
C 1 4 8 7 12
D 5 2 9 11 4
dfB:
A B C D
X 4 12 32 11,4
Y 1 6 2 9,3
Z 2 8,5 11 1,4
dfC:
A1 A2 A3 A4 A150
X A A A A A
Y C A C A B
Z D D D A D
The Big-O notation of FFT Multiplication is O(nlogn). What is Big-O notation of a FFT multiplication under a loop as given in algorithm below? The code is given in matlab and FFTmulti is a function for FFT Multiplication of two polynomials
rG=1;
rN=1;
AreaFunc=[1 2 5 2 3 6 7 2 4 5 6];
N=length(AreaFunc);
for i=1:(N-1)
ref_coeff(i) = (AreaFunc(i+1) - AreaFunc(i)) / (AreaFunc(i+1) + AreaFunc(i));
end
ref_coeff=[ref_coeff rN];
G = (1 + rG) / 2;
A0 = [1]; B0 = [-rG];
for i = 1 : length(ref_coeff)
G = G * (1 + ref_coeff(i));
A1 = [-ref_coeff(i) 0]; B1 = [1 0];
An = [0 A0] + FFTmulti(A1,B0);
Bn = [0 -ref_coeff(i)*A0] + FFTmulti(B1,B0);
A0=An;
B0=Bn;
end
A0 =fliplr(A0);
num = zeros(1, (floor(N/2)));
num = [num G];
FFT complexity -for the best known optimization algorithms - is N*log2(N).
If you call it inside a loop of N, will be N^2 log2(N).