Dynamic Neo4j Cypher Query - sql

Is there a way to write the below neo4j cypher script to handle n case whens depending on the size of the array being read? I have varying array sizes on which to calculate co2 consumption so to use a one size fits all case when would be highly inefficient.
MATCH paths = allShortestPaths((a: Flights {label: 'Paris'})-[: FLIGHT*]->(b:Flights {label: 'Sydney'}))
WITH paths, relationships(paths) AS rels
UNWIND rels AS rel
WITH paths,
collect(rel.co2) AS co2,
collect(rel.engine_consumption) as ec
RETURN
reduce(acc1=0.0,
x IN range(0, size(fc)-1) |
case when x=0 then acc1 + co2[x]
when x=1 then acc1 + co2[x] * ec[x-1]
when x=2 then acc1 + co2[x] * ec[x-1] * ec[x-2]
when x=3 then acc1 + co2[x] * ec[x-1] * ec[x-2] * ec[x-3]
...
when x=size(ec)-1 then acc1 + co2[x] * ec[x-1] * ... * ec[0]
end
) AS normalised_co2
;

I did this in the end using the python library py2neo, and created the cypher query using python
case_when = ''
accumulator = ''
for x in range(max_paths):
accumulator += f'* co2[{x}]'
case_when += f'when x={x+1} then acc + co2[{x+1}]' + accumulator + ' \n '

Related

DAX If Value is 0 then dont use it in calculation

I have four columns with respective weights (0.1667 or 0.3333) and if one of them (Ta, Tb, Tc, Td) are 0 I don't want them to be involved in my end calculation. Can someone help me with the denomiator part since it needs to be dynamic regarding the output?
var Tb= ('T2'[Col2] * 0.1667)
var Tc = ('T3'[Col3] * 0.3333)
var Td = ('T4'[Col4] * 0.3333)
Return end_calculation = Ta + Tb + Tc + Td / 0.1667 + 0.1667 + 0.3333 + 0.3333 ```
(Only take all four weights in the denomiator into account if it is not empty)
Use the IF function, and use DIVIDE to add support for divide-by-zero.
divide(Ta + Tb + Tc + Td, if(Ta>0,0.1667,0) + ...

What is the Big-O of this nested loop?

int z=1;
for(int i=0;i*i<n;i++){
z*=3;
for(int j=0;j<z;j++){
// Some code
}
}
Answer is O(3^n).
Is it correct? How to figure out time complexity of nested loop?
outer loop: i goes from 1 to sqrt(n);
inner loop: j,z goes up to 3^(sqrt(n));
"some code" will run 1 + 3 + 3^2 + ... + 3^(sqrt(n)) times
let sum = 1 + 3 + 3^2 + ... + 3^(sqrt(n))
sum - 3*sum = 1 - 3(sqrt(n) + 1)
sum = 1 - 3(sqrt(n) + 1) / (1-3) = 2( 3^(sqrt(n)+1) - 1 )
2( 3^(sqrt(n)+1) - 1 ) << O( 3^sqrt(n) )
O(3^sqrt(n)) is more accurate
You could approach the problem using Sigma notation this way:

SQL: computation of depth inside stock's glass

Little description of task:
Need to get total of base if we will selling coin_quantity of coin to the market.
By default, this example means, that market is coin+"_"+base. No flip case.
For simplification, there is only one case(action): selling / ask.
python:
def get_depth_price( db_cursor, coin_quantity, coin, market ):
sql_command = "SELECT * FROM '" + market + "' WHERE type=1"
db_cursor.execute( sql_command )
bids = sorted([ i for i in db_cursor.fetchall() ], \
key = lambda k: k[3], reverse = True )
tmp_quantity = float( coin_quantity )
tmp_total = 0
for bid in bids:
if tmp_quantity > bid[4]:
tmp_total += bid[4] * bid[3]
tmp_quantity -= bid[4]
else:
tmp_total += tmp_quantity * bid[3]
tmp_quantity = 0
break
return tmp_total
Where market data looks like:
# echo "SELECT * from 'DOGE_BTC' WHERE type=1
ORDER BY price DESC;" | sqlite3 big.db
6f827564d88ddd0d99a9f976ac384a3f|0|1|5.0e-07|374365.08
1f696fea1270c07d9d4217e47ad40d3c|0|1|4.9e-07|1337443.42857
b9bee0a3bc2d4b241383062f06569b54|0|1|4.8e-07|465618.375
716cb29e0f5fe4742de73302e5b88250|0|1|4.7e-07|197560.659574
3189ed55c60530014892c6a3fce673e8|0|1|4.6e-07|115757.521739
cf19858241fb25de9095160b1704ef44|0|1|4.5e-07|237807.133333
f53642c0e7d5074daaa2b324e82483c5|0|1|4.4e-07|16112.6818182
ee8fb3f5255fb0ef8c157becb6a8c539|0|1|4.3e-07|22581.0697674
### (0)id ----^ (1)self---^ ^ ^-(3)price ^
### (2)type(ask=0/bid=1)-------+ (4)quantity--+
Methinks, that python script - is very very slow thing against sql.
However, I can't create similar on the sql language.
How to replace full function of get_depth_price by sql-query?
How to create on sql similar to if tmp_quantity > bid[4] / then / else ?

Recurrence Relation without using Master Theorem

I can easily solve some recurrence relations using the master theorem but I want to understand how to solve them w/o using the theorem
EX:
T(n) = 5T(n/2) + O(n) T(1) =1
Answer: O(n^{log_2(5)}
Expanding,
T(n) = 5T(n/2) + cn = 5(5T(n/4) + c(n/2)) + cn =
..... = 5^i * T(n/(2^i)) + cn*(1 + (5/2) + (5/2)^2 +......+ (5/2)^i)
Now let i= log_2(n)
then
5^(log_2(n)) * T(1) + cn*(1 + (5/2) + (5/2)^2 +......+ (5/2)^(log_2(n)))
After this I am lost . How do I get something similar to n^{log_2(5)?
Update:
Using the formula for the sum of geometric series (Sum = a* (1-r^n)/(1-r))
I get Sum = 1*(1-(5/2)^{log_2(n)})/(-3/2) = 2/3*c*(5^{log_2(n)} - n
How are 5^{log_2(n)} and n^{log_2(5)} related?
Thanks :D
I did not check the rest of your calculation, but note that
a^b = exp(b * ln(a))
and
log_b(a) = ln(a) / ln(b)
And thus
5^{log_2(n)} = exp(log_2(n) * ln(5)) = exp(ln(n) / ln(2) * ln(5))
and also
n^{log_2(5)} = exp(ln(5) / ln(2) * ln(n))

Vectorizing Code for efficient implementation

The following is an IIR code. I need to vectorize the code so that I can write NEON code efficiently.
Example of vectorization
Non vectorized code
for(i=0;i<100;i++)
a[i] =a[i]*b[i]; //only one independent multiplication cannot take
//advantage of multiple multiplication units
Vectorized code
for(i=0;i<25;i++)
{
a[i*4] =a[i*4]*b[i*4]; //four independent multiplications can use
a[(i+1)*4] =a[(i+1)*4]*b[(i+1)*4]; // multiple multiplication units to perform the
a[(i+2)*4] =a[(i+2)*4]*b[(i+2)*4]; //operation in parallel
a[(i+3)*4] =a[(i+3)*4]*b[(i+3)*4];
}
Please help me in vectorizing the for loop below so as to implement the code efficiently by using the vector capability of hardware (my hardware can perform 4 multiplications simultaneously).
main()
{
for(j=0;j<NUMBQUAD;j++)
{
for(i=2;i<SAMPLES+2 ;i++)
{
w[i] = x[i-2] + a1[j]* w[i-1] + a2[j]*w[i-2];
y[i-2] = w[i] + b1[j]* w[i-1] + b2[j]*w[i-2];
}
w[0]=0;
w[1] =0;
}
}
Once you have fixed (or verified) the equations, you should notice that there are 4 independent multiplications in each round of the equation. The task becomes in finding the proper and least number of instructions to permute input vectors x[...], y[...], w[...] to some register
q0 = | w[i-1] | w[i-2] | w[i-1] | w[i-2]|
q1 = | a1[j] | a2[j] | b1[j] | b2[j] | // vld1.32 {d0,d1}, [r1]!
q2 = q0 .* q1
A potentially much more effective method of wavefront parallelism can be achieved by inverting the for loops.
x0 = *x++;
w0 = x0 + a*w1 + b*w2; // pipeline warming stage
y0 = w0 + c*w1 + d*w2; //
[REPEAT THIS]
// W2 = W1; W1 = W0;
W0 = y0 + A*W1 + B*W2;
Y0 = W0 + C*W1 + D*W2;
// w2 = w1; w1 = w0;
x0 = *x++;
*output++= Y0;
w0 = x0 + a*w1 + b*w2;
y0 = w0 + c*w1 + d*w2;
[REPEAT ENDS]
W0 = y0 + A*W1 + B*W2; // pipeline cooling stage
Y0 = W0 + C*W1 + D*W2;
*output++= Y0;
While there are still dependencies between x0->w0->y0->W0->Y0, there's an opportunity of full 2-way parallelism in between lower-case and upper-case expressions. Also one can try to get rid of shifting the values w2=w1; w1=w0; by unrolling the loop and doing manual register renaming.