Conditional Linq Expression Tree - linq

I want a Linq Expression which dynamically compiles at runtime
I have a value and if than value greater than say for e.g. 5000 and another value > 70 then it should return a constant x
else
value greater than say 5000 and another value < 70 it returns y
How do I create an expression tree
a > 5000 & b < 70 then d
else
a > 5000 & b > 70 then e

You can use a lambda expression with the ternary operator (?:).
var d = 1;
var e = 2;
var f = 3;
Expression<Func<int,int,int>> expression =
(a, b) => (a > 5000 && b < 70) ? d :
(a > 5000 && b > 70) ? e :
f; // If b == 70
var func = expression.Compile();
var val = func(5432, 1);

Related

Reverse the isometric projection algorithm

I've got this code:
const a = 2; // always > 0 and known in advance
const b = 3; // always > 0 and known in advance
const c = 4; // always > 0 and known in advance
for (let x = 0; x <= a; x++) {
for (let y = 0; y <= b; y++) {
for (let z = 0; z <= c; z++) {
for (let p = 0; p <= 1; p++) {
for (let q = 0; q <= 2; q++) {
let u = b + x - y + p;
let v = a + b + 2 * c - x - y - 2 * z + q;
let w = c + x + y - z;
}
}
}
}
}
The code generates (a+1)*(b+1)*(c+1)*2*3 triplets of (u,v,w), each of them is unique. And because of that fact, I think it should be possible to write reversed version of this algorithm that will calculate x,y,z,p,q based on u,v,w. I understand that there are only 3 equations and 5 variables to get, but known boundaries for x,y,z,p,q and the fact that all variables are integers should probably help.
for (let u = ?; u <= ?; u++) {
for (let v = ?; v <= ?; v++) {
for (let w = ?; w <= ?; w++) {
x = ?;
y = ?;
z = ?;
p = ?;
q = ?;
}
}
}
I even managed to produce the first line: for (let u = 0; u <= a + b + 1; u++) by taking the equation for u and finding min and max but I'm struggling with moving forward. I understand that min and max values for v are depending on u, but can't figure out the formulas.
Examples are in JS, but I will be thankful for any help in any programming language or even plain math formulas.
If anyone is interested in what this code is actually about - it projects voxel 3d model to triangles on a plain. u,v are resulting 2d coordinates and w is distance from the camera. Reversed algorithm will be actually a kind of raytracing.
UPDATE: Using line equations from 2 points I managed to create minmax conditions for v and code now looks like this:
for (let u = 0; u <= a + b + 1; u++) {
let minv = u <= a ? a - u : -a + u - 1;
let maxv = u <= b ? a + 2 * c + u + 2 : a + 2 * b + 2 * c - u + 3;
for (let v = minv; v <= maxv; v++) {
...
}
}
I think I know what to do with x, y, z, p, q on the last step so the problem left is minw and maxw. As far as I understand those values should depend both on u and v and I must use plane equations?
If the triplets are really unique (didn't check that) and if p and q always go up to 1 and 2 (respectively), then you can "group" triplets together and go up the loop chain.
We'll first find the 3 triplets that where made in the same "q loop" : the triplets make with the same x,y,z,p. As only q change, the only difference will be v, and it will be 3 consecutive numbers.
For that, let's group triplets such that, in a group, all triplets have the same u and same w. Then we sort triplets in groups by their v parameters, and we group them 3 by 3. Inside each group it's easy to assign the correct q variable to each triplet.
Then reduce the groups of 3 into the first triplet (the one with q == 0). We start over to assign the p variable : Group all triplets such that they have same v and w inside a group. Then sort them by the u value, and group them 2 by 2. This let's us find their p value. Remember that each triplet in the group of 3 (before reducing) has that same p value.
Then, for each triplet, we have found p and q. We solve the 3 equation for x,y,z :
z = -1 * ((v + w) - a - b - 3c -q)/3
y = (w - u + z + b - c - p)/2
x = u + y - b - p
After spending some time with articles on geometry and with the huge help from Wolfram Alpha, I managed to write needed equations myself. And yes, I had to use plane equations.
const a = 2; // always > 0 and known in advance
const b = 3; // always > 0 and known in advance
const c = 4; // always > 0 and known in advance
const minu = 0;
const maxu = a + b + 1;
let minv, maxv, minw, maxw;
let x, y, z, p, q;
for (let u = minu; u <= maxu; u++) {
if (u <= a) {
minv = a - u;
} else {
minv = -a + u - 1;
}
if (u <= b) {
maxv = a + 2 * c + u + 2;
} else {
maxv = a + 2 * b + 2 * c - u + 3;
}
for (let v = minv; v <= maxv; v++) {
if (u <= b && v >= a + u + 1) {
minw = (-a + 2 * b - 3 * u + v - 2) / 2;
} else if (u > b && v >= a + 2 * b - u + 2) {
minw = (-a - 4 * b + 3 * u + v - 5) / 2;
} else {
minw = a + b - v;
}
if (u <= a && v <= a + 2 * c - u + 1) {
maxw = (-a + 2 * b + 3 * u + v - 1) / 2;
} else if (u > a && v <= -a + 2 * c + u) {
maxw = (5 * a + 2 * b - 3 * u + v + 2) / 2;
} else {
maxw = a + b + 3 * c - v + 2;
}
minw = Math.round(minw);
maxw = Math.round(maxw);
for (let w = minw; w <= maxw; w++) {
z = (a + b + 3 * c - v - w + 2) / 3;
q = Math.round(2 - (z % 1) * 3);
z = Math.floor(z);
y = (a + 4 * b + q - 3 * u - v + 2 * w + 3) / 6;
p = 1 - (y % 1) * 2;
y = Math.floor(y);
x = (a - 2 * b - 3 * p + q + 3 * u - v + 2 * w) / 6;
x = Math.round(x);
}
}
}
This code passes my tests, but if someone can create better solution, I would be very interested.

Better approach for too many if else statement

if ( x < 275 && x >= 0 )
f = 275
else if ( x < 450 && x >= 275) (right side comparison always previous left side default comparison value
f = 450
else if ( x < 700 && x >= 450)
f = 700
else if ( x < 1000 && x >= 700)
f = 1000
..... and more
is there any way or mathematical formula approach to eliminate this multiple if else statement for less code require?
Here is something in C (but easy enough to port to almost anything). It misses a couple of conditions (x < 0 and x > "biggest known") It doesn't add much value when you only have 4 values, but the more values you have, the more code this removes. Note that it will slow things down, but doubtful that you'd notice it unless you had a huge list of possible values.
int getF(int x)
{
/* possibleValues must be sorted in ascending order */
int possibleValues[] = { 275, 450, 700, 1000 };
for(int i = 0; i < sizeof(possibleValues) / sizeof(possibleValues[0]); i++)
{
int pv = possibleValues[i];
if (x < pv)
{
return pv;
}
}
/* fall thru "error" handle as you see fit */
return x;
}
if(x>0)
{
if(x<275)
f = 275;
else if(x<450)
f = 450;
else if(x<700)
f = 700;
else if(x<1000)
f = 1000;
else //and so on
Assuming that x>0. Just avoiding if-else, this also shall suffice the conditions -
x<275 ? 275 : (x<450 ? 450 : (x<700 ? 700 : (x<1000 ? 1000 :
Though if there is any defined type range of inputs like INT, BIG etc instead of 275, 450.. you can check the type of input. Also you can so the same using iteration as suggested by #John3136.

positional sum of 2 numbers

How to sum 2 numbers digit by digit with pseudo code?
Note: You don't know the length of the numbers - if it has tens, hundreds, thousands...
Units should be add to units, tens to tens, hundreds to hundreds.....
If there is a value >= 10 in adding the units you need to put the value of that ten with "the tens"....
I tried
Start
Do
Add digit(x) in A to Sum(x)
Add digit(x) in B to Sum(x)
If Sum(x) > 9, then (?????)
digit(x) = digit(x+1)
while digit(x) in A and digit(x) in B is > 0
How to show the result?
I am lost with that.....
Please help!
Try this,
n = minDigit(a, b) where a and b are the numbers.
let sum be a number.
m = maxDigit(a,b)
allocate maxDigit(a,b) + 1 memory for sum
carry = 0;
for (i = 1 to n)
temp = a[i] + b[i] + carry
// reset carry
carry = 0
if (temp > 10)
carry = 1
temp = temp - 10;
sum[i] = temp
// one last step to get the leftover carry
if (digits(a) == digits(b)
sum[n + 1] = carry
return
if (digits(a) > digits(b)
toCopy = a
else
toCopy = b
for (i = n to m)
temp = toCopy[i] + carry
// reset carry
carry = 0
if (temp > 10)
carry = 1
temp = temp - 10;
sum[i] = temp
Let me know if it helps
A and B are the integers you want to sum.
Note that the while loop ends when all the three integers are equal to zero.
carry = 0
sum = 0
d = 1
while (A > 0 or B > 0 or carry > 0)
tmp = carry + A mod 10 + B mod 10
sum = sum + (tmp mod 10) * d
carry = tmp / 10
d = d * 10
A = A / 10
B = B / 10

override moveTo and moveByPx methods of OpenLayers.Map

How to override moveTo and moveByPx methods of OpenLayers.Map for eliminate "movestart" event triggering for any actions except zooming ?
map = new OpenLayers.Map("map");
OpenLayers.Map.prototype.moveByPx = function (a, b) {
var c = this.size.w / 2,
d = this.size.h / 2,
e = c + a,
f = d + b,
g = this.baseLayer.wrapDateLine,
h = 0,
k = 0;
this.restrictedExtent && (h = c, k = d, g = !1);
a = g || e <= this.maxPx.x - h && e >= this.minPx.x + h ? Math.round(a) : 0;
b = f <= this.maxPx.y - k && f >= this.minPx.y + k ? Math.round(b) : 0;
if (a || b) {
this.dragging || (this.dragging = !0);
this.center = null;
a && (this.layerContainerOriginPx.x -= a, this.minPx.x -= a, this.maxPx.x -= a);
b && (this.layerContainerOriginPx.y -= b, this.minPx.y -= b, this.maxPx.y -= b);
this.applyTransform();
d = 0;
for (e = this.layers.length; d < e; ++d)
c = this.layers[d], c.visibility && (c === this.baseLayer || c.inRange) && (c.moveByPx(a, b), c.events.triggerEvent("move"));
this.events.triggerEvent("move")
}
}
map.events.register("movestart", map, function (e) {
My Code...
});

An operator riddle

While converting a project from Python to C#, I found some interesting differences in the syntax families. Yet I got stuck, still unable to understand and comprehend the dissimilar behavior of comparison operator in C#.
During the course of curing this curiosity, I considered few languages of C-syntax family; C, C++, C#, Java, Javascript.. and verified the behavior. Here is how it transpired:
Let a=2, b=3, c=4, d=5;
Now, consider the following expressions:
a < a < a // returns true
c < b < a // returns true
c > b > a // returns false
a < c > b // returns false
If it was due to the right-associativity, then the following code in JavaScript shouldn't act like:
console.info(a < false); // returns false
console.info(a < a); // returns false
console.info(a < a < a); // returns true, as opposed to returning false
Here is the C/C++ version
int main(){
int a=2, b=3, c=4, d=5;
printf("%s\n","false\0true"+6*(a < a < a)); // returns true
printf("%s\n","false\0true"+6*(c < b < a)); // returns true
printf("%s\n","false\0true"+6*(c > b > a)); // returns false
printf("%s\n","false\0true"+6*(a < c > b)); // returns false
return 0;
}
Except in Python, where
a < a < a // returns false
c < b < a // returns false
c > b > a // returns true
a < c > b // returns true
Can anyone explain why C-family of languages and Python are computing the expressions differently?
Because Python uses a slightly interpretation of your input:
Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.
This means your lines will be interpreted as
a < a < a = a < a and a < a // returns false
c < b < a = c < b and b < a // returns false
c > b > a = c > b and b > a // returns true
a < c > b = a < c and c > b // returns true
In C-style languages, an comparison expression will evaluate to either false (integer value 0) or true (integer value 1). So in C it will behave like
a < a < a = (a < a) < a = 0 < a // returns true
c < b < a = (c < b) < a = 0 < a // returns true
c > b > a = (c > b) > a = 1 > a // returns false
a < c > b = (a < c) > b = 0 > b // returns false
Note that almost all languages define operators with a boolean return value, but since boolean values can be implicit converted to zero or one the proposition above is still valid:
// C++ example
struct myComparableObject{
int data;
bool operator<(const myComparableObject& o){
return data < o.data;
}
};
myComparableObject a, b;
a.data = 2;
b.data = 3;
int c = 5;
a < b; // true
a < c; // error, a cannot be converted to int / unknown operator
a.data < c; // true
a < b < c; // true, as this is equal to
// (a < b) < c = false < c = 0 < c
For example JavaScript will actually use ToNumber in order to compare two non-string objects, see [ECMAScript p78, 11.8.5 The Abstract Relational Comparison Algorithm], where ToNumber(false) is zero and ToNumber(true) === 1.
The comparison x < y, where x and y are values, produces true, false, or undefined[...]
Let px be the result of calling ToPrimitive(x, hint Number).
Let py be the result of calling ToPrimitive(y, hint Number).
If it is not the case that both Type(px) is String and Type(py) is String, then
a. Let nx be the result of calling ToNumber(px). Because px and py are primitive values evaluation
order is not important.
b. Let ny be the result of calling ToNumber(py).
c. If nx is NaN, return undefined.
d. If ny is NaN, return undefined.
e. If nx and ny are the same Number value, return false.
f. If nx is +0 and ny is -0, return false.
g. If nx is -0 and ny is +0, return false.
h. If nx is +infty, return false.
i. If ny is +infty, return true.
j. If ny is -infty, return false.
k. If nx is -infty, return true.
l. If the mathematical value of nx is less than the mathematical value of ny —note that these
mathematical values are both finite and not both zero— return true. Otherwise, return false.
It's because a < a < a is evaluated like ((a < a) < a) which then becomes 0 < a which is true (when a >= 0)
If you run the following, the first expression changes to false.
int main(){
int a=0, b=3, c=4, d=5;
printf("%s\n","false\0true"+6*(a < a < a)); // returns false
printf("%s\n","false\0true"+6*(c < b < a)); // returns true
printf("%s\n","false\0true"+6*(c > b > a)); // returns false
printf("%s\n","false\0true"+6*(a < c > b)); // returns false
return 0;
}
Whereas in Python, a < a < a becomes a < a and a < a. So it's not comparing the result of a < a. If you add parentheses to your statements, you'll get the C-like behavior again.
(a < a) < a ## returns true
(c < b) < a ## returns true
(c > b) > a ## returns false
(a < c) > b ## returns false

Resources