Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Can someone help with the following MATH question.
Keep in mind the operator precedence.
x = 27 * 24 \ 4 / 2 – 4 + 2
Arithmetic and logical operators are evaluated in the following order of precedence:
Exponentiation (^)
Negation (-)
Multiplication and division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
What is the result?
x = ((27 * 24) \ (4 / 2)) - 4 + 2
= (648 \ 2) - 4 + 2
= 324 - 4 + 2
= 322
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
My code:
import Pkg
Pkg.add("PyPlot")
using PyPlot
e = 0.0
eta = 1e - 4
wmin = -2.0
wmax = 2.0
Nw = 1000
w =range(wmin, wmax, Nw)
g = 1 ./ (w .- e .+ eta * im)
t = 1.0
g2 = g .^2
Gsemi = (1 ./ (g*2*t^2)) .* (1 .- sqrt(1 .- 4*t^2*g .^2)) //Error line
Ginf = Gsemi ./ (1 .- Gsemi .^ 2 * t^2)
xlabel(L"Energy $\omega $", fontsize=20)
ylabel("Density of states", fontsize=20)
axis([-2,2,0,1.4])
plot(w, (-1.0/pi)*imag(Ginf), linewidth=3.0)
The error message:
How can I fix this? sorry my english is bad, thank for anwers
This particular error happens because you cannot take the square root of a vector, since it does not make sense mathematically.
But you can take the square root of each element separately. To do that add a dot to the sqrt function call:
sqrt.(1 .- 4*t^2*g .^2)
But you should really add more dots on that line to save some temporary allocations. You can do that with the #. macro instead of dotting everything by hand, like this:
Gsemi = #. (1 / (g*2*t^2) * (1 - sqrt(1 - 4*t^2*g^2)))
Now, all operations will be element-wise.
Also, make sure to put your code inside a function.
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 5 years ago.
Improve this question
My apologies, this question is now redirected to
this web page in math forum.
Empirically, I can know that (a+b+c) mod 2 = (a-b-c) mod 2.
e.g.,)
1+2+3 = 6, 6 mod 2 = 0
1-2-3 = -4, -4 mod 2 = 0
1+2+4 = 7, 7 mod 2 = 1
1-2-4 = -5, -5 mod 2 = 1
It seems that it is only possible when we use binary modulo (mod 2).
Is there any formal proof for this?
Not sure, why this ended up on SO. As James said in the comments, these questions should be asked on math.stackexchange But since it is here:
I a + b + c = a - b - c + 2(b + c)
II 2(b + c) ≡ 0 (mod 2), ergo
III a + b + c ≡ a - b - c (mod 2)
Edit, since it was requested: The generalisation of II would require n to be a divisor of 2 to fulfill
2(b + c) ≡ 0 (mod n)
for all b and c, which means that n is either 1 or 2.
The reason this works mod 2 is exactly because there are only two residues: 0 and 1. And thus it is true that for any x
x ≡ -x mod 2
Thus a + b ≡ a - b mod 2
Obviously this is not true for any other modulo operation. So for any other n > 2 you can create a simple counter-example for (a+b+c) ≡ (a-b-c) mod n:
a = n
b = 0
c = 1
(a + b + c) mod n = 1
(a - b - c) mod n = n - 1
Obviously n - 1 is not equal to 1 if n > 2. Actually most of the triplets (a, b, c) would be counter-examples for any n > 2.
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 7 years ago.
Improve this question
I used PEMDAS on 3 - 1 * 8 + 2 * 3.
Steps:
1) 1 * 8 = 8
2) 2 * 3 = 6
3) 8 + 6 = 14
4) 3 - 14 = -11
Multiply all the terms, then add and finally subtract but I get -11 as the result.
But when I googled it, it said 1. Where did I go wrong?
By Following the BODMAS rule -
3 - 1 * 8 + 2 * 3
So According to BODMAS Rule-
B → Brackets first (parentheses)
O → Of (orders i.e. Powers and Square Roots, Cube Roots, etc.)
DM → Division and Multiplication (start from left to right)
AS → Addition and Subtraction (start from left to right)
So our equation ->
3 - (1 * 8) + (2 * 3)
3-8+6
-5+6
1
After multiply all the terms,please follow the order of operations.Right steps:
Steps: 1) 1 * 8 = 8 2) 2 * 3 = 6 3)3-8=-5 4)-5+6=1
The order of operations is, as you said it PEMDAS (or BEDMAS whatever you like), but it is also left-to-right. And on top of that: multiplication and division are treated as the same order and so are addition and subtraction.
So your first two steps were right.
3 - (1 * 8) + (2 * 3)
3 - 8 + 6
Now here is where the left-to-right ordering takes place.
((3 - 8) + 6)
(-5 + 6)
1
To make it easier, you can remember that x - y is really just x + (-y). Then the order of subtraction and addition doesn't matter at all.
You are not following the order of operations correctly. You are correct in doing the multiplication first. However, you also must always go from left to right when performing operations of the same type (addition and subtraction or multiplication and division). By using parenthesis, we can make this clearer.
3 - (1*8) + (2*3) => 3 - 8 + 6 => -5 + 6 => 1
Your mistake is in step 3. The - gets applied to 8 so step 3 is: -8 + 6 = -2
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a an unbalanced dataset with intraday data and would like to columnwise split it into a matrix depending on the day the given observation is from.
Short example follows. A matrix A:
A = [1 5; 1 5; 1 6; 2 4; 2 2; 3 8; 3 4];
where the first column is the day indicator and the second an observed value, should be transformed into a matrix B:
B = [5 4 8; 5 2 4; 6 nan nan];
I don't care if it's solved in Matlab, R or excel, it's a one time thing for me.
Thanks,
J.
A solution in Matlab:
A = [1 5; 1 5; 1 6; 2 4; 2 2; 3 8; 3 4];
nMaxDays = max(A(:, 1));
nMaxSamples = max(accumarray(A(:, 1), 1));
mnSamplesMatrix = nan(nMaxSamples, nMaxDays);
for (nDay = unique(A(:, 1))')
vnThisDay = A(A(:, 1) == nDay, 2);
mnSamplesMatrix(1:numel(vnThisDay), nDay) = vnThisDay;
end
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I came across this in some brain teaser question bank.
What should be the answer of this?
( (3+1) / 3) * 6
I thought it is 7 but the correct answer is 8.
That can happen only when it is (3+1) * (6/3). Since there are parenthesis given in the expression how can I get 8? I tried to double check and the answer is correct.
Can somebody help me in understanding this?
Working with integers:
((3 + 1) / 3) * 6
= (4 / 3) * 6
= 1 * 6
= 6
Working with floats:
((3 + 1) / 3) * 6
= (4 / 3) * 6
= (4 / 3) * (6 / 1)
As a fraction:
= (6 * 4) / (3 * 1)
= 24 / 3
= 8
This is just simple arithmetic - nothing to do with programming, nor the difference between infix, prefix and postfix notations:
((3+1) / 3) * 6 = (4 / 3) * 6
= 1.333... * 6
= 8
This brain teaser question bank is clearly expecting you to use the usual rules of mathematics - it's not a C brain teaser question bank after all. Even if you were using integer division instead (where 4 / 3 would give 1), the answer would be 6.
See the result on Wolfram Alpha.
By the sound of it, this is basic arithmetic and has nothing whatsoever to do with programming:
(3 + 1) / 3 * 6 =
4 / 3 * 6 =
(4 * 6) / 3 =
24 / 3 =
8