Coffeescript: conditional value one-liner - coffeescript

Can this be turned into a snazzy one-liner?
thing = thing2 / 32 * 100 * 100
thing = 0 unless thing

Unfortunately coffeescript doesn't support ternary operator. But you can write:
#zoomGlobal = if #position.ratio then #position.ratio / 32 * 100 * 100 else 0
Or
#zoomGlobal = #position.ratio / 32 * 100 * 100 || #zoomGlobal
Here example

if you are confident that #position.ratio is always set as a number, you could just have:
#zoomGlobal = #position.ration / 32 * 100 * 100
soon you should be able to use the new ** operator
#zoomGlobal = #position.ration / 32 * 100 ** 2
if #position.ration might not exist then use what Ivan suggested:
#zoomGlobal = if #position.ratio then #position.ratio / 32 * 100 * 100 else 0
also, I can't see from your example what the context is but note that
10 / 2 * 2 * 2 == 20
while
10 / (2 * 2 * 2) == 1.25

Related

how to solve subscript out of range in qbasic

I am in big problem in solving a code essential for me, and I need the solution as soon as possible
in fact, I have little knowledge of programming in basic
I have a problem with this code.
I have an equation, and I use this code to solve this equation
when I run the program
this error appears
subscript out of range
is there any solution to this problem
0 Print "******** impact *******"
20 Print "____________"
30 Print "this programs is used to solve impact integral"
40 Print "equation of simply supported slab to "
50 Print "optain the following"
60 Print " (1) force _time history"
70 Print " (2) central deflection - time history"
80 Print "-----------------"
90 Print "input data:"
100 Print " (1) FUNDAMENTAL NATURAL FREQUANCY (RAD/SEC)--- W1"
110 Print " (2) STRIKER MASS (KG.) ----- Mst"
120 Print " (3) MASS OF SLAB (KG)---- Ms"
130 Print " (4) hertz constant (n/m^1.5)----k"
140 Print " (5) STRICKER VELOCITY (M/S)----Vo"
150 Print " (6) NUMBER OF MODES----N"
160 Print "_________"
170 Input " W11, MST, MS, K, VO, N", W11, MST, MS, K, VO, N
180 Print "W1="; W11; "RAD/SEC"
190 Print "MST="; MST; "KG"
200 Print "MS="; MS; "KG"
210
220 Print " K = "; K; "N/M^1.5"
230 Print STANDARD
240 Print "VO="; VO; "M/S"
241 Print " N = "; N
250 Print
260 Print
270 K1 = K
280 V = VO
290 W1 = W11 / 2
300 TINF = 2.94 * (MST / (.8 * K1 * V ^ .5)) ^ .4 * 1000
310 DT = TINF / 10
320 M = 20
330 DT = PROUND(DT, 0)
340 DT = DT / 1000
350 M = 20
360 Option Base 1
370 Dim W(11, 11), Z(11, 11), F(30), D(30), BM(30), SH(30), A(30), T(30), DF(30), S(11, 11, 30), C(11, 11, 30)
380 ReDim W(N, N), Z(N, N)
390 For I = 1 To N Step 2
400 For K = 1 To N Step 2
410 W(K, I) = W1 * (I ^ 2 + K ^ 2)
420 Z(K, I) = W(K, I) * DT
430 Next K
440 Next I
450 ReDim F(M), D(M), A(M), T(M), DF(M), BM(M), SH(M), S(N, N, M), C(N, N, M)
460 F(1) = D(1) = A(1) = T(1) = 0
470 For I = 1 To N Step 2
480 For K = 1 To N Step 2
490 S(K, I, 1) = C(K, I, 1) = 0
500 Next K
510 Next I
520 B1 = 0
530 For I = 1 To N Step 2
540 For K = 1 To N Step 2
550 B1 = B1 + (1 - Sin(Z(K, I)) / Z(K, I)) / W(K, I) ^ 2
560 Next K
570 Next I
580 B = -DT ^ 2 / (6 * MST) - 4 * B1 / MS
590 VE = 0
600 For I = 2 To M
610 T(I) = (I - 1) * DT
620 GM = 0
630 If VE = 1 Then 970
640 For J = 2 To I
650 GM = GM + F(J - 1)
660 Next J
670 AA = 0
680 For J = 1 To N Step 2
690 For K = 1 To N Step 2
700 FF = F(I - 1) * (Sin(Z(K, J)) / Z(K, J) - Cos(Z(K, J))) / W(K, J)
710 AA = AA + 4 * (Cos(Z(K, J)) * S(K, J, I - 1) + Sin(Z(K, J)) * C(K, J, I - 1) + FF) / (MS * W(K, J))
720 Next K
730 Next J
740 A(I - 1) = V * (I - 1) * DT - (D(I - 1) + DT ^ 2 * (GM - F(I - 1) / 6)) / MST - AA
750 F = F(I - 1)
760 If A(I - 1) + B * F < 0 Then 840
770 F1 = (A(I - 1) + B * F) ^ 1.5 * K1
780 X = Abs(F1 - F)
790 If X < 10 Then 820
800 F = F1
810 GoTo 770
820 F(I) = F1
830 GoTo 850
840 F(I) = 0
850 D(I) = D(I - 1) + DT ^ 2 * (GM + (F(I) - F(I - 1)) / 6)
860 For J = 1 To N Step 2
870 For K = 1 To N Step 2
880 S(K, J, I) = Cos(Z(K, J)) * S(K, J, I - 1) + Sin(Z(K, J)) * C(K, J, I - 1) + (1 - Sin(Z(K, J)) / Z(K, J)) * (F(I) - F(I - 1)) / W(K, J) + (1 - Cos(Z(K, J))) * F(I - 1) / W(K, J)
890 C(K, J, I) = Cos(Z(K, J)) * C(K, J, I - 1) - Sin(Z(K, J)) * S(K, J, I - 1) + (1 - Cos(Z(K, J))) / Z(K, J) * (F(I) - F(I - 1)) / W(K, J) + Sin(Z(K, J)) * F(I - 1) / W(K, J)
900 Next K
910 Next J
920 DF = 0
930 For J = 1 To N Step 2
940 For K = 1 To N Step 2
950 DF = DF + 4 * S(K, J, I) / W(K, J) / MS
960 Next K
970 Next J
980 DF(I) = DF
990 If F(I) = 0 Then 1010
1000 Next I
1010 Print "----------------------------------------------------------"
1020 Print "{TIME (MS)},{FORCE (KN)},{DEFLECTION(MM)}"
1030 Print "----------------------------------------------------------"
1040 II = I
1050 For O = 1 To II
1060
1070 Print Tab(1); ":"; Tab(5); T(0) * 1000; Tab(18); ":"; Tab(22); F(O) / 1000; Tab(34); ":"; Tab(42); DF(O) * 1000; Tab(56); ":"
1080 Print "-----------------------------------------------------------"
1090 Next O
1100 End

Precedence operator between * and / in PHP

$c = 8 / (12 - 8) * 4;
echo $c; //output 8
I read in http://php.net/manual/en/language.operators.precedence.php. I think that it should be 8/16 instead of 8. Are there anybody help me explaination, thanks.
$c = 8 / (12 - 8) * 4;
echo $c; //output 8
Because (12 - 8) equal 4 it will execute first. After this execution we will get
$c = 8 / 4 * 4;
After that the result will be 2 * 4 php will execute 8 / 4 and give a result 2.
Finally the result will be 8.
In this code Parentheses will get first priority, then the division and finally multiplication.
operators' precedence order from high to low:/ == * > -
But () will enforce priority to the highest.
Operators with the same precedence, operations order will be from left to right.
so, the operations process is:
<?php
$c = 8 / (12 - 8) * 4;
// Give priority to parentheses inside
$c = 8 / 4 * 4; // 12 - 8 ==> 4
// operators with precedence, from left to right
$c = 2 * 4; // 8 / 4 ==> 2
$c = 8; // 2 * 4 ==> 8
This involves only the basic mathematical arithmetic

how to solve simultaneous equations with solve and with result without roots

Here is the code:
syms G1 G2 G3 M1 M2 M3 P1 P2 P3 D1 D2 D3
S = solve(0 == 0.9 * (20 - G1) - 0.012 * D3 * G1, ...
0 == 0.9 * (20 - G2) - 0.012 * D1 * G2, ...
0 == 0.9 * (20 - G3) - 0.012 * D2 * G3, ...
0 == -0.0033 * M1 + 0.002 * G1, ...
0 == -0.0033 * M2 + 0.002 * G2, ...
0 == -0.0033 * M3 + 0.002 * G3, ...
0 == 0.1 * M1 - 0.0033 * P1 + 2 * 0.5 * D1 - 2 * 0.025 * (P1 ^ 2), ...
0 == 0.1 * M2 - 0.0033 * P2 + 2 * 0.5 * D2 - 2 * 0.025 * (P2 ^ 2), ...
0 == 0.1 * M3 - 0.0033 * P3 + 2 * 0.5 * D3 - 2 * 0.025 * (P3 ^ 2), ...
0 == -0.5 * D1 + 0.025 * (P1 ^ 2) + 0.9 * (20 - G2) - 0.012 * D1 * G2, ...
0 == -0.5 * D2 + 0.025 * (P2 ^ 2) + 0.9 * (20 - G3) - 0.012 * D2 * G3, ...
0 == -0.5 * D3 + 0.025 * (P3 ^ 2) + 0.9 * (20 - G1) - 0.012 * D3 * G1)
The problem is that I need real solution, but I have got answers with roots. How can I get real answers?
Assuming by "real solution" you mean a numeric value instead of the exact root-of-polynomial form, there are two options depending on how you intend on using the results:
double: this will evaluate the RootOf expressions, assuming there are no free parameters, and return a numeric output of class double. While the output is now limited to double-precision, if your goal is to use the roots in computation and care about performance, this is the fastest option.
vpa: this will evaluate the RootOf expressions, assuming there are no free parameters, and return a Symbolic output of class sym. While the output is now able to be approximated to varying degrees of accuracy at evaluation by calling digits beforehand, there may be a large computational overhead in subsequent calculations due to its Symbolic nature.

Solve special system of linear equations in Matlab (GNU Octave)

I have a matrix, let's say 5x5 looking like this:
0 0 0 1 0
0 0 0 4/5 1/5
3/5 1/5 1/5 0 0
1/5 2/5 1/5 1/5 0
1/10 1/10 2/5 1/5 1/5
I need it to solve it like a system of linear equations looking like this (I can transpose it myself, but then multiplying it with the symbolic variables gets me into troubles):
0 * a + 0 * b + 3/5 * c + 1/5 * d + 1/10 + e = a
0 * a + 0 * b + 1/5 * c + 2/5 * d + 1/10 + e = b
0 * a + 0 * b + 1/5 * c + 1/5 * d + 2/5 + e = c
1 * a + 4/5 * b + 0 * c + 1/5 * d + 1/5 + e = d
0 * a + 1/5 * b + 0 * c + 0 * d + 1/5 + e = e
a + b + c + d + e = 1
I can easily solve this in wxMaxima, but I have to manually write all the values there, which is increasingly tedious with bigger matrices.
Is there a way to get the results after some steps using matlab operator \ for solving system of linear equations?
You can solve the equation set no?
>>[A-eye(5);ones(1,5)]\[0,0,0,0,0,1]'
ans =
0.1729
0.2061
0.1345
0.4350
0.0515
>> sum(ans)
ans =
1.0000
And a symbolic solution:
M=sym(A);
v=sym('[a;b;c;d;e]');
sol=solve(M*v==v,sum(v)==1);
returns solutions in the form sol.a, sol.b, ...

Using importdata() to automatically change headerlines from file to file

Okay so I have multiple files that I'm trying to import for analysis, and I'm using uiimport() to do this within a script currently, but I have to manually shift the headerlines for each file as they change every time. I would like to do something like this:
DELIMITER = ',';
HEADERLINES = 50;
data_imported = importdata(file_to_get, DELIMITER, HEADERLINES);
But the issue is that only 1 or 2 of my 300~ files has a headerline count of 50. Another very important note, is that the default number of headerlines for EVERY file only needs to have 2 added to it. So if the default headerline number while using uiimport() is 50, then i would need to change it to 52.
Would there be a way to change this with possibly an if statement? Note: defaultheader is being used as and example and not an actual variable.
for example:
if defaultheader = 30
header = 32
end
example of what the .CSV file looks like in excel
*Title
*
*Description (1.7) Tue 03/20/12 09:59:09
*
* file name
*
* TestNames:
* _Test 1 (ein: 10)
* _Test 2 (ein: 15)
* _Test 3 (ein: 20)
* _Test 4 (ein: 25)
* _test 5 (ein: 30)
* _test 6 (ein: 35)
* _test 7 (ein: 40)
* _test 8 (ein: 45)
* _test 9 (ein: 50)
* _test 10 (ein: 55)
* _test 11 (ein: 60)
* _test 12 (ein: 65)
* _test 13 (ein: 70)
* _test 14 (ein: 75)
* _test 15 (ein: 80)
* _test 16 (ein: 85)
*
* Info1: ...
* Info2: ...
* info3: ...
* info4: ...
* info5: ...
* info6: ...
* info7: ...
* info8: ...
* info9: ...
* info10: ...
* info11: ...
* info12: ...
*
* Pixel,Tap Name,Tap Pixel,brightness
,,,10,15,20,25,30,35,40
,,,0,67,21,12,223,231,832
1,0A1,458,75,89,24,46,256,763,532
2,0A1,457,43,65,56,554,263,254,732
3,0A1,456,28,47,76,221,412,732,832
4,0A1,455,12,23,36,466,652,633,637
5,0A1,454,11,78,98,678,864,241,223
You could use TEXTSCAN function to read the CSV files, and specify the CommentStyle option to ignore all header lines starting with a * character.
The code below will first read the file as a cell array of lines, ignoring all lines starting with *. We also skip the first two lines immediately after the header section. Next, we loop over each line, and parse the tab-delimited values as string. We ignore the first three columns, and convert the remaining values to numeric. Finally we store that row in the matrix M. Note that the code does not assume we know the number of columns in advance, and instead determine that from one of the data rows.
%# read lines
fid = fopen('file.csv','rt');
C = textscan(fid, '%s', ...
'Delimiter','', 'Whitespace','\n', 'CommentStyle','*');
fclose(fid);
C = C{1}(3:end); %# skip two lines
%# parse each line
numCols = sum(C{1}==',')+1 - 3; %# number of columns
M = zeros(numel(C),numCols);
for i=1:numel(C)
v = textscan(C{i}, '%s', 'Delimiter',',');
v = str2double(v{1}(4:end));
M(i,1:numel(v)) = v;
end
The resulting matrix M containing the part of the file you are interested in:
M =
75 89 24 46 256 763 532
43 65 56 554 263 254 732
28 47 76 221 412 732 832
12 23 36 466 652 633 637
11 78 98 678 864 241 223