Power BI while loop - while-loop

I'm trying to do a while loop in Power BI M language. But all of the logic are all over my head!
How would you translate a very simple loop like this into M language?
while X == True:
do abcdef
if Y == True:
end
Thanks very much!

Loops in M are probably best handled with the List.Generate function.
This article does a pretty good job at explaining how it works:
https://potyarkin.ml/posts/2017/loops-in-power-query-m-language/
Using this function, let's look at a more specific implementation of a while loop, say to find Fibonacci numbers less than 1000.
a = 1
b = 1
while b < 1000
b = a + b
a = b - a
would translate to M something like this:
let
data =
List.Generate(
() => [ a = 1, b = 1 ],
each [b] < 1000,
each [ b = [a] + [b], a = [b] ]
),
output = Table.FromRecords(data)[a]
in output
I'm not sure the best way to handle your break condition Y. It might depend on the specific problem.

Related

How do I properly calculate this time complexity

I'm examining this code as preparation for my test and I'm having some problems figuring out what is the correct time complexity:
a = 1;
while (a < n) {
b = 1;
while (b < a^2)
b++;
a = a*2;
}
The values for a are as follows : 1, 2, 4, 8, ... , 2^(logn) = n
Therefore we have logn iterations for the outer loop.
In every nested loop, there are a^2 iterations, so basically what I've come up with is:
T(n) = 1 + 4 + 16 + 64 + ... + (2^logn)^2
I'm having problems finding the general term of this series and therefore getting to a final result.
(maybe due to being completely off in my calculations though)
Would appreciate any help, thank you.
Your calculations are alright, you are correct with your analysis of the inner while-loop. We can demonstrate this with a small table:
This is basically the sum of a geometric progression with:
a1 = 1, q = 4, #n = lg n + 1
Where #n is the number of elements.
We have: Sn = 1 * (4^(lgn +1) - 1)/(4-3) = (4*n^2 - 1)/3
Thus we can say your code running complexity is Θ(n^2)
Mathematical explanation in LaTeX:

Question with proving unboundedness in Linear Programming

I would like to prove the following: "For a Linear Programming in standard form with constraint Ax = b and all variables >= 0 show that d is a direction of unboundedness if and only if Ad = 0 and all entries in d >= 0. Please help.
I highly recommend Bertsekas - Introduction to Linear Optimization, since it deals with Linear Programming in a graphical and intuitive way. It also contains the proof you seek.
A few hints:
If Ad = 0, and Ax = b, then A(x + td) = b for t >= 0;
Then, if d >= 0, what does this say about x + td? Does it ever become smaller than 0?
Now, the other way around:
If d is a direction for unboundedness, what happens if any d < 0?
Similarly, what happens if Ad != 0?

Name the output of an expression in Tensorflow

I wonder whether it's possible to give a name to the output of a certain expression to retrieve it from the graph at another part of the code.
For example
def A(a, b):
c = a + b
d = d * a
return d
For debug purposes it would be nice if I could pull out c at another position without returning it through the entire function hierarchy.
Any ideas?
Thanks in advance!
I'm assuming a and b are tensors.
Either you give a name to c using tf.identity
def A(a, b):
c = a + b
c = tf.identity(c, name = "c")
d = d * a
return d
Either you use the tf.add operation instead of +:
def A(a, b):
tf.add(a, b, name = "c")
d = d * a
return d
Either way, you get retrieve c with tf.get_variable('c:0') (You might need to precise the scope if any.)

Inconsistency in sql query output using OR after WHERE

I'm doing a query on a complex db:
SELECT *
FROM
table1,
table2,
table3,
table4,
table5,
table6,
table7,
table8
WHERE
a = b and
c = d and
e = d and
(
(strfldvar = 'BROKEN_ARROW' AND x = g)
OR (strfldvar = 'BROKEN_BOX' AND y = g)
) and
f = h and
i = j
Only works when strfldvar = 'BROKEN_BOX' and not when strfldvar = 'BROKEN_ARROW'. When I replace
(
(strfldvar = 'BROKEN_ARROW' AND x = g)
OR (strfldvar = 'BROKEN_BOX' AND y = g)
) and
with either x = g and or y = g and it works fine in two seperate queries runs like that. The error message for the case strfldvar = 'BROKEN_ARROW' is:
ORA-01013: user requested cancel of current operation
Before this error message comes the computer goes into deep thought for I guess 2 minutes.
What am I doing wrong here?
f.y.i. I looked at the names of the fields of the of the two seperate runs and they appear idendical. I mean the scema of the output looks the same for both. But I'm not 100% sure they are the same, if that matters i.e.
Thanks for your help
When strfldvar = 'BROKEN_ARROW' AND x = g (or if strfldvar is not BROKEN_ARROW or BROKEN_BOX), the y = g part is not evaluated, which seems to be causing the query to run for longer than you expect - until it's eventually killed by you, your client or resource limits. I suspect that's the only join condition for whichever table y is from, so you end up with a cartesian product.
When strfldvar = 'BROKEN_BOX' then both x = g and y = g will be evaluated, so you wouldn't get the same cartesian product, against either of the tables providing x and y.
If you are essentially deciding which table to include in the query based on that flag then you'll need to redesign this; possibly with a union of two queries, one which joins to x and the other on y; or with separate queries and you decide which to run; or maybe even with outer joins. But it depends on what you're really trying to do and what the data looks like. The code you have shown is a too generic to guess what will be appropriate.

Hoare Logic, while loop with '<= '

I'm working on some Hoare logic and I am wondering whether my approach is the right one.
I have the following program P:
s = 0
i = 1
while (i <= n) {
s = s + i
i = i + 1
}
It should satisfy the hoare triple {n >= 0}P{s = n*(n+1)/2} (so it just takes the sum). Now, initially I had |s = i*(i-1)/2| as my invariant, which works fine. However, I had a problem from going to the end of my loop, to my desired postcondition. Because for the impliciation
|s = i*(i-1)/2 & i > n|
=>
| s = n * (n+1) / 2 |
to hold, I need to prove that i is n+1, and not just any i bigger than n. So what I thought of is to add a (i <= n + 1) to the invariant, so that it becomes :
|s = i * (i-1)/2 & i <= n+1|
Then I can prove the program so I think it should be correct.
Nonetheless, I find the invariant to be a bit, less "invariantly" :). And not like anything I've seen in the course or in the exercises so far, so I was wondering if there was a more elegant solution here?
So what I thought of is to add a (i <= n + 1) to the invariant, so that it becomes :
|s = i * (i-1)/2 & i <= n+1|
Nonetheless, I find the invariant to be a bit, less "invariantly" :). And not like anything I've seen in the course or in the exercises so far, so I was wondering if there was a more elegant solution here?
Nope, given the way the code is written, that's exactly the way to go. (I can tell from experience since I've been teaching Hoare logic during several semesters in two different courses and since it's part of my graduate studies.)
Using i <= n is common practice when programming. In your particular program, you could just as well have written i != n+1 instead, in which case your first invariant (which indeed looks cleaner) would have sufficed since you get
| s=i*(i-1)/2 & i=n+1 |
=>
| s=n*(n+1)/2 |
which evidently holds.
There is another way to reason,given a more appropriate invariant (and other code)...searh n for final value of i...
I : s = i*(i+1)/2 and 0 <= i <=n
B : i < n
Now,evidently you have for post condition:
I and i >= n => s = i*(i+1)/2 and i=n => s = n*(n+1)/2
The code now becomes
s = 0
i = 0
while (i < n) {
s = s + (i+1)
i = i + 1
}
The invariant holds at init and keeps after each loop,since rewriting I as 2s=i*(i+1) we have to proof
I and i<n => 2(s + (i+1)) = (i+1)*(i+2)
2(s + (i+1) )=
2s + 2(i+1) =
i*(i+1) + 2(i+1)= (since I holds)
(i+1)(i+2)
Qed.