Sum on spotifre - math

I try to compare two expressions in an array on spotfire
but they do not give me the same result and I don't understand why from a mathematical point of view.
Sum([OUTS_P] - [OUTS_P2])
Sum([OUTS_P]) - Sum([OUTS_P2])
Do you have an idea in which case these two operations could be different ?

take this example table:
A B
1 3
2 2
3 1
we have these two results:
Sum([A]) - Sum([B]) = Sum(1, 2, 3) - Sum(3, 2, 1) = 6 - 6 = 0
Sum([A] - [B]) = Sum( (1 - 3), (2 - 2), (3 - 1) ) = Sum(-2, 0, 2) = 0
this is what you're expecting, and this will work 100% of the time.
unless, of course, your table resembles this one:
A B
1 3
2
3 1
B:2 is NULL or (Empty). this table results in the expressions being evaluated as:
Sum([A]) - Sum([B]) = Sum(1, 2, 3) - Sum(3, 1) = 6 - 4 = 2
Sum([A] - [B]) = Sum( (1 - 3), (3 - 1) ) = Sum(-2, 2) = 0
the reason is because NULL is non-numeric; it's not possible to evaluate 2 - NULL, and this data is ignored by Sum().
if you want both expressions to always result in the same answer, you can create a calculated column like this for each column you'll be using in Sum():
If([Column] is NULL, 0, [Column])
and then aggregate on this column instead of the original.

Related

Get a sequence number from 0 and alternate positive/negative incrementing every other time

I would like to be able to obtain a (non-convergent) sequence of numbers by a simple calculation that would look like this: 0, 1, -1, 2, -2, 3, -3, 4, -4 ...
By simple calculation I mean being able to do it with a single variable that would start from 1 (or 0) without having to rearrange this sequence.
I made several (unsuccessful) attempts in Lua, here is what it should look like in principle (this example only alternates 0s and 1s):
do
local n = 0
for i = 1, 10 do print(n)
n = n==0 and 1 or -n + (n/n)
end
end
Is this possible and how?
Update:
I just succeeded like this:
local n, j = 0, 2
for i = 1, 10 do print(n)
n = n==0 and 1 or j%2==0 and -(n+(n/math.abs(n))) or -n
j = j + 1
end
But I have to help myself with a second variable, I would have liked to know if with only n it would be possible to do it?
The whole numbers are enumerable. Thus there exists a mapping from the natural numbers to whole numbers. You'll now have to use a loop to loop over natural numbers, then compute a function that gives you a whole number:
-- 0, 1...10, -1...-10 -> 21 numbers total
for n = 1, 21 do
local last_bit = n % 2
local sign = 1 - (2 * last_bit)
local abs = (n - last_bit) / 2
print(sign * abs)
end
prints
-0
1
-1
2
-2
...
10
-10
on Lua 5.1; on newer Lua versions, you can use n // 2 instead of (n - last_bit) / 2 to (1) use ints and (2) make extracting the abs cheaper.
Simply "emit" both n and -n in each iteration:
for n = 0, 10 do
print(n)
print(-n)
end
My problem was solved by #EgorSkriptunoff in comment of my question, his approach is:
n = (n > 0 and 0 or 1) - n
The output of:
local n = 0
for i=1,10 do io.write(n..", ")
n = (n > 0 and 0 or 1) - n
end
Actually gives:
0, 1, -1, 2, -2, 3, -3, 4, -4, 5,

Is there a function f(n) that returns the n:th combination in an ordered list of combinations without repetition?

Combinations without repetitions look like this, when the number of elements to choose from (n) is 5 and elements chosen (r) is 3:
0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 2 3
1 2 4
1 3 4
2 3 4
As n and r grows the amount of combinations gets large pretty quickly. For (n,r) = (200,4) the number of combinations is 64684950.
It is easy to iterate the list with r nested for-loops, where the initial iterating value of each for loop is greater than the current iterating value of the for loop in which it is nested, as in this jsfiddle example:
https://dotnetfiddle.net/wHWK5o
What I would like is a function that calculates only one combination based on its index. Something like this:
tuple combination(i,n,r) {
return [combination with index i, when the number of elements to choose from is n and elements chosen is r]
Does anyone know if this is doable?
You would first need to impose some sort of ordering on the set of all combinations available for a given n and r, such that a linear index makes sense. I suggest we agree to keep our combinations in increasing order (or, at least, the indices of the individual elements), as in your example. How then can we go from a linear index to a combination?
Let us first build some intuition for the problem. Suppose we have n = 5 (e.g. the set {0, 1, 2, 3, 4}) and r = 3. How many unique combinations are there in this case? The answer is of course 5-choose-3, which evaluates to 10. Since we will sort our combinations in increasing order, consider for a minute how many combinations remain once we have exhausted all those starting with 0. This must be 4-choose-3, or 4 in total. In such a case, if we are looking for the combination at index 7 initially, this implies we must subtract 10 - 4 = 6 and search for the combination at index 1 in the set {1, 2, 3, 4}. This process continues until we find a new index that is smaller than this offset.
Once this process concludes, we know the first digit. Then we only need to determine the remaining r - 1 digits! The algorithm thus takes shape as follows (in Python, but this should not be too difficult to translate),
from math import factorial
def choose(n, k):
return factorial(n) // (factorial(k) * factorial(n - k))
def combination_at_idx(idx, elems, r):
if len(elems) == r:
# We are looking for r elements in a list of size r - thus, we need
# each element.
return elems
if len(elems) == 0 or len(elems) < r:
return []
combinations = choose(len(elems), r) # total number of combinations
remains = choose(len(elems) - 1, r) # combinations after selection
offset = combinations - remains
if idx >= offset: # combination does not start with first element
return combination_at_idx(idx - offset, elems[1:], r)
# We now know the first element of the combination, but *not* yet the next
# r - 1 elements. These need to be computed as well, again recursively.
return [elems[0]] + combination_at_idx(idx, elems[1:], r - 1)
Test-driving this with your initial input,
N = 5
R = 3
for idx in range(choose(N, R)):
print(idx, combination_at_idx(idx, list(range(N)), R))
I find,
0 [0, 1, 2]
1 [0, 1, 3]
2 [0, 1, 4]
3 [0, 2, 3]
4 [0, 2, 4]
5 [0, 3, 4]
6 [1, 2, 3]
7 [1, 2, 4]
8 [1, 3, 4]
9 [2, 3, 4]
Where the linear index is zero-based.
Start with the first element of the result. The value of that element depends on the number of combinations you can get with smaller elements. For each such smaller first element, the number of combinations with first element k is n − k − 1 choose r − 1, with potentially some of-by-one corrections. So you would sum over a bunch of binomial coefficients. Wolfram Alpha can help you compute such a sum, but the result still has a binomial coefficient in it. Solving for the largest k such that the sum doesn't exceed your given index i is a computation you can't do with something as simple as e.g. a square root. You need a loop to test possible values, e.g. like this:
def first_naive(i, n, r):
"""Find first element and index of first combination with that first element.
Returns a tuple of value and index.
Example: first_naive(8, 5, 3) returns (1, 6) because the combination with
index 8 is [1, 3, 4] so it starts with 1, and because the first combination
that starts with 1 is [1, 2, 3] which has index 6.
"""
s1 = 0
for k in range(n):
s2 = s1 + choose(n - k - 1, r - 1)
if i < s2:
return k, s1
s1 = s2
You can reduce the O(n) loop iterations to O(log n) steps using bisection, which is particularly relevant for large n. In that case I find it easier to think about numbering items from the end of your list. In the case of n = 5 and r = 3 you get choose(2, 2)=1 combinations starting with 2, choose(3,2)=3 combinations starting with 1 and choose(4,2)=6 combinations starting with 0. So in the general choose(n,r) binomial coefficient you increase the n with each step, and keep the r. Taking into account that sum(choose(k,r) for k in range(r,n+1)) can be simplified to choose(n+1,r+1), you can eventually come up with bisection conditions like the following:
def first_bisect(i, n, r):
nCr = choose(n, r)
k1 = r - 1
s1 = nCr
k2 = n
s2 = 0
while k2 - k1 > 1:
k3 = (k1 + k2) // 2
s3 = nCr - choose(k3, r)
if s3 <= i:
k2, s2 = k3, s3
else:
k1, s1 = k3, s3
return n - k2, s2
Once you know the first element to be k, you also know the index of the first combination with that same first element (also returned from my function above). You can use the difference between that first index and your actual index as input to a recursive call. The recursive call would be for r − 1 elements chosen from n − k − 1. And you'd add k + 1 to each element from the recursive call, since the top level returns values starting at 0 while the next element has to be greater than k in order to avoid duplication.
def combination(i, n, r):
"""Compute combination with a given index.
Equivalent to list(itertools.combinations(range(n), r))[i].
Each combination is represented as a tuple of ascending elements, and
combinations are ordered lexicograplically.
Args:
i: zero-based index of the combination
n: number of possible values, will be taken from range(n)
r: number of elements in result list
"""
if r == 0:
return []
k, ik = first_bisect(i, n, r)
return tuple([k] + [j + k + 1 for j in combination(i - ik, n - k - 1, r - 1)])
I've got a complete working example, including an implementation of choose, more detailed doc strings and tests for some basic assumptions.

Impact of the parameter dimension in gather function

I am trying to use the gather function in pytorch but can't understand the role of dim parameter.
Code:
t = torch.Tensor([[1,2],[3,4]])
print(torch.gather(t, 0, torch.LongTensor([[0,0],[1,0]])))
Output:
1 2
3 2
[torch.FloatTensor of size 2x2]
Dimension set to 1:
print(torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]])))
Output becomes:
1 1
4 3
[torch.FloatTensor of size 2x2]
How, gather function actually works?
I realized how the gather function works.
t = torch.Tensor([[1,2],[3,4]])
index = torch.LongTensor([[0,0],[1,0]])
torch.gather(t, 0, index)
Since the dimension is zero, so the output will be:
| t[index[0, 0], 0] t[index[0, 1], 1] |
| t[index[1, 0], 0] t[index[1, 1], 1] |
If the dimension is set to one, the output will become:
| t[0, index[0, 0]] t[0, index[0, 1]] |
| t[1, index[1, 0]] t[1, index[1, 1]] |
So the formula is:
For a 3-D tensor the output is specified by:
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2
Reference: http://pytorch.org/docs/master/torch.html?highlight=gather#torch.gather
Just add to the existing answer, one application of gather is to collect scores along a designated dimension.
For instance we have such settings:
3 classes and 5 examples
Each class is assigned of a score, do it for every example
Objective is to collect scores indicated by the labels y
The code is as the follows
torch.manual_seed(0)
num_examples = 5
num_classes = 3
scores = torch.randn(5, 3)
#print of scores
scores: tensor([[ 1.5410, -0.2934, -2.1788],
[ 0.5684, -1.0845, -1.3986],
[ 0.4033, 0.8380, -0.7193],
[-0.4033, -0.5966, 0.1820],
[-0.8567, 1.1006, -1.0712]])
y = torch.LongTensor([1, 2, 1, 0, 2])
res = scores.gather(1, y.view(-1, 1)).squeeze()
Outputs:
#print of gather results
tensor([-0.2934, -1.3986, 0.8380, -0.4033, -1.0712])

Time complexity of doubly recursive function

So this is the code:
int test ( int n)
{
if (n ≤2) return 1;
else return test(n-2) * test(n-2);
}
I'm not confident in how to reason about this recursive function. I tried mapping the N value to the recursion depth like so:
N = 2 -> 0 recursions
N = 4 -> 2
N = 8 -> 14
But to be honest I'm not sure this gets me anywhere (and just thinking about test(16) hurts my head.
Let's start by writing out a recurrence relation for the total number of calls made:
T(0) = T(1) = T(2) 1, since there's one total call (the initial call).
T(n+2) = 2T(n) + 1, since there's one call for the initial call, plus two recursive calls to problems of size n.
Let's start by looking at the case where n is even. Then
T(0) = 1
T(2) = 1
T(4) = 2T(2) + 1 = 3
T(6) = 2T(4) + 1 = 7
T(8) = 2T(6) + 1 = 15
T(9) = 2T(8) + 1 = 31
Except for the 0 case, it looks like these values take on the pattern 1, 3, 7, 15, 31, etc. Notice that each of these is one less than a power of two: 1 = 2 - 1, 3 = 4 - 1, 7 = 8 - 1, etc. We can guess that what we're looking at has something to do with powers of two.
Going back to our sequence, we might make a guess that
T(2) = 1 = 21 - 1
T(4) = 3 = 22 - 1
T(6) = 7 = 23 - 1
...
T(2n) = 2n - 1
So if n is even, we have T(n) = 2n/2 - 1 = (√2)n - 1. You can formalize this using a proof by induction.
For the odd case, we basically get the same thing:
T(1) = 1
T(3) = 2T(1) + 1 = 3
T(5) = 2T(3) + 1 = 7
T(7) = 2T(5) + 1 = 15
T(9) = 2T(7) + 1 = 31
...
T(2n+1) = 2n - 1
So if n is even, then T(n) = 2(n-1)/2 - 1. Again, you can prove this by induction to formalize things if you'd like.

using CLISP to make equations given a sequence of numbers

so i have to use clisp to create 2 equaline equations given a sequence of numbers
IE user enters 2 2 2 2:
2 + 2 = 2 + 2 ; would be valid
2 - 2 = 2 - 2 ; would also be valid
2 = 2 + 2 - 2 ; valid
2 + 2 + 2 = 2 ; not valid
user enters 6 2 2 2:
6 = 2 + 2 + 2 ; valid
6 = 2 * 2 + 2 ; valid
6 + 2 = 2 * 2 ; not valid
The operates of *, /, +, and - are to be used for basic math, and = to signify that Left Hand Side = Right Hand Side.
My problem lies in my lack of any real lisp training and where to start. I think I would have to use macros, but I'm not sure how to use macros or how macros would be used for this.
I know that initially I would have to define a function such as
(defun findequation (a b c d))
But from there I'm lost
First step: create all combinations of the relevant symbols, with the restriction that exactly one = should be present.
Example input: (2 2 2 2), (+ - * / =)
Example output: (2 + 2 + 2 = 2), (2 + 2 - 2 = 2), (2 - 2 - 2 = 2), …
Second step: use the shunting-yard algorithm to transform the infix list of alternating numbers and symbols into a tree.
Example input: (2 + 2 = 2 - 2)
Example output: (= (+ 2 2) (- 2 2))
Then you can evaluate that to see if it is true.

Resources