I have range from 0 to a which I have to proportionally map to a range from b to c. How to achieve this in NetLogo.
0 to a -> b to c
[0 a] -> [b c]
assuming your input is x, then:
b + (x / a) * (c - b)
Related
I have three variables : A, B, C.
I want to write a set of linear equations such that
X = 1 if atleast 2 of A,B,C are ones.
X= 0 if only one of A,B,C is one.
X = 0 if all of them are zero.
A,B and C are binary (0,1).
Kindly suggest a linear equation for this.
Thank you.
To implement
A+B+C ≥ 2 => X=1
A+B+C ≤ 1 => X=0
we can write simply:
2X ≤ A+B+C ≤ 2X+1
A,B,C,X ∈ {0,1}
In practice, you may have to formulate the sandwich equation as two inequalities.
X >= A + B - 1
X >= B + C - 1
X >= A + C - 1
X <= A + B
X <= B + C
X <= A + C
as mentioned in comment, you didn't define outcome for A=B=C=0, but in that case X -> 0 by inspection.
Working in Swift, ARTKit / SceneKit
I have a line AB in 3d and I have xyz coordinates of both points A and B.
I also have a point C and I know its xyz coordinates too.
Now, I want to find out the xyz coordinates of point D on line AB; given that CD is perpendicular to AB.
What would be a simple way to do it in Swift.
Parameterize the line AB with a scalar t:
P(t) = A + (B - A) * t`
The point D = P(t) is such that CD is perpendicular to AB, i.e. their dot product is zero:
dot(C - D, B - A) = 0
dot(C - A - (B - A) * t, B - A) = 0
dot(C - A, B - A) = t * dot(B - A, B - A)
// Substitute value of t
--> D = A + (B - A) * dot(C - A, B - A) / dot(B - A, B - A)
Swift code:
var BmA = B - A
var CmA = C - A
var t = dot(CmA, BmA) / dot(BmA, BmA)
var D = A + BmA * t;
Suppose that gcd(e,m) = g. Find integer d such that (e x d) = g mod m
Where m and e are greater than or equal to 1.
The following problem seems to be solvable algebraically but I've tried doing it and it give me an integer number. Sometimes, the solution for d is an integer and sometimes it isn't. How can I approach this problem?
d can be computed with the extended euklidean algorithm, see e.g. here:
https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
The a,b on that page are your e,m, and your d will be the x.
Perhaps you are assuming that both e and m are integers, but the problem allows them to be non-integers? There is only one case that gives an integer solution when both e and m are integers.
Why strictly integer output is not a reasonable outcome if e != m:
When you look at a fraction like 3/7 say, and refer to its denominator as the numerator's "divisor", this is a loose sense of the word from a classical math-y perspective. When you talk about the gcd (greatest common divisor), the "d" refers to an integer that divides the numerator (an integer) evenly, resulting in another integer: 4 is a divisor of 8, because 8/4 = 2 and 2 is an integer. A computer science or discrete mathematics perspective might frame a divisor as a number d that for a given number a gives 0 when we take a % d (a mod d for discrete math). Can you see that the absolute value of a divisor can't exceed the absolute value of the numerator? If it did, you would get pieces of pie, instead of whole pies - example:
4 % a = 0 for a in Z (Z being the set of integers) while |a| <= 4 (in math-y notation, that set is: {a ∈ Z : |a| <= 4}), but
4 % a != 0 for a in Z while |a| > 4 (math-y: {a ∈ Z : |a| > 4}
), because when we divide 4 by stuff bigger than it, like 5, we get fractions (i.e. |4/a| < 1 when |a| > 4). Don't worry too much about the absolute value stuff if it throws you off - it is there to account for working with negative numbers since they are integers as well.
So, even the "greatest" of divisors for any given integer will be smaller than the integer. Otherwise it's not a divisor (see above, or Wikipedia on divisors).
Look at gcd(e, m) = g:
By the definition of % (mod for math people), for any two numbers number1 and number2, number1 % number2 never makes number1 bigger: number1 % number2 <= number1.
So substitute: (e * d) = g % m --> (e * d) <= g
By the paragraphs above and definition of gcd being a divisor of both e and m: g <= e, m.
To make (e * d) <= g such that d, g are both integers, knowing that g <= e since g is a divisor of e, we have to make the left side smaller to match g. You can only make an integer smaller with multiplcation if the other multipland is 0 or a fraction. The problem specifies that d is an integer, so we one case that works - the d = 0 case - and infinitely many that give a contradiction - contradiction that e, m, and d all be integers.
If e == m:
This is the d = 0 case:
If e == m, then gcd(e, m) = e = m - example: greatest common divisor of 3 and 3 is 3
Then (e * d) = g % m is (e * d) = m % m and m % m = 0 so (e * d) = 0 implying d = 0
How to code a function that will find d when either of e or m might be NON-integer:
A lot of divisor problems are done iteratively, like "find the gcd" or "find a prime number". That works in part because those problems deal strictly with integers, which are countable. With this problem, we need to allow e or m to be non-integer in order to have a solution for cases other than e = m. The set of rational numbers is NOT countable, however, so an iterative solution would eventually make your program crash. With this problem, you really just want a formula, and possibly some cases. You might set it up like this:
If e == m
return 0 # since (e * d) = m % m -> d = 0
Else
return g / e
Lastly:
Another thing that might be useful depending on what you do with this problem is the fact that the right-hand-side is always either g or 0, because g <= m since g is a divisor of m (see all the stuff above). In the cases where g < m, g % m = g. In the case where g == m, g % m = 0.
The #asp answer with the link to the Wikipedia page on the Euclidean Algorithm is good.
The #aidenhjj comment about trying the math-specific version of StackOverflow is good.
In case this is for a math class and you aren't used to coding: <=, >=, ==, and != are computer speak for ≤, ≥, "are equal", and "not equal" respectively.
Good luck.
I want to return the indices of elements which satisfy some conditions and the condition that their index should be between some constants A and B. There is a naive form of implementing this with:
inds=find(conditions)
real_inds=find(A<=inds<=B)
but it is inefficient and actually I want to limit my search to elements with index between those constants, not all elements.
how about restricting yourself to the range A, B?
Suppose you have my_vector and you wish to find the elements larger than 0.3 and smaller than 0.5 (the "conditions"). Limiting yourself to the range A, B is simply:
masked_ind = find(my_vector(A:B) > 0.3 & my_vector(A:B) < 0.5);
real_ids = masked_ind + A - 1; %// correct the offset induced by `A`.
By applying the conditions on my_vector(A:B) you actually don't care how big is my_vector and you are only processing elements in the range A:B.
BTW, have you considered using logical indexing, as suggested by Andras Deak, instead of using find and the actual linear indices?
%// create a mask to keep indices from A to B only
real_inds_logical = false(size(your_vector));
real_inds_logical(A:B) = (my_vector(A:B)>0.3 & my_vector(A:B)<0.5);
You can do something like this:
Suppose that you have vector x, conditions: x == 7 or ( x > 3 and x < 5) and you want the search between A and B.
Now define vector g as an auxiliar vector of indices:
g = 1:length(x);
And then get your indices like this:
indices = g( (g >= A) & (g <= B) & (conditions) );
That in this case is translated to:
indices = g( (g >= A) & (g <= B) & (x == 7 | (x > 3 & x < 5)) );
This will return the elements of g that satisfice the conditions between the extern parenthesis.
An example code:
Initial values:
x = [0.0975 0.2785 0.5469 0.9575 0.9649 0.1576 0.9706 0.9572 0.4854 0.8003];
A = 4;
B = 9;
Conditions: x >= 0.1 and x <= 0.7
Code:
g = 1:length(x);
indices = g((g >= A) & (g <= B) & (x <= 0.7) & (x >= 0.1));
Result:
indices =
6 9
How can I use the Jacobian written below as function from (x, y) ?
g := (x, y) -> x - y
u := (x, y) -> x^2 + y^2
J := jacobian([g(x, y), u(x, y)], [x, y]);
My idea was to make funcion like this
Jf := (u, v) -> subs(x = u, y = v, J(x, y))
but it returns ugly matrix with brakets inside.
P. S. I use Maple 17
The linalg package (which exports the jacobian command) and lowercase matrix are deprecated. Use LinearAlgebra and Matrix instead, and VectorCalculus:-Jacobian.
Also, note the use of unapply.
restart:
g := (x, y) -> x - y:
u := (x, y) -> x^2 + y^2:
J:=VectorCalculus:-Jacobian([g(x,y),u(x,y)],[x,y]);
[ 1 -1 ]
J := [ ]
[2 x 2 y]
Jf:=unapply(J,[x,y]):
Jf(1,1);
[1 -1]
[ ]
[2 2]
Jf(s,t);
[ 1 -1 ]
[ ]
[2 s 2 t]