How to perform a cumulative summation based on multiple variables in a running array in Excel? - dynamic-arrays

Hi All, this is my first post and hope everyone could guide me along the way.
In the above screen, I'm trying to find a formula to be put in Col F such that
it sums up the direction "up" or "down" in Col E in the given running order of Col E
it is able to handle the change in direction from "up" to "down", vice versa and sum up the running array accordingly.
Eg. Cell F6 is the summation of D3:D6 (criteria basis E3:E6). If we look at F16, it's the summation of D15:D16 only (criteria basis E15:E16).
I have tried searching all over the web for a formula to handle my need but unable to find any "smart" formula to handle a moving array? This could be too complexed for my level. Appreciate if anyone could shed some light on whether my requirement is feasible.
Thanks in advance.

Related

Is it possible to represent 'average value' in programming?

Had a tough time thinking of an appropriate title, but I'm just trying to code something that can auto compute the following simple math problem:
The average value of a,b,c is 25. The average value of b,c is 23. What is the value of 'a'?
For us humans we can easily compute that the value of 'a' is 29, without the need to know b and c. But I'm not sure if this is possible in programming, where we code a function that takes in the average values of 'a,b,c' and 'b,c' and outputs 'a' automatically.
Yes, it is possible to do this. The reason for this is that you can model the sort of problem being described here as a system of linear equations. For example, when you say that the average of a, b, and c is 25, then you're saying that
a / 3 + b / 3 + c / 3 = 25.
Adding in the constraint that the average of b and c is 23 gives the equation
b / 2 + c / 2 = 23.
More generally, any constraint of the form "the average of the variables x1, x2, ..., xn is M" can be written as
x1 / n + x2 / n + ... + xn / n = M.
Once you have all of these constraints written out, solving for the value of a particular variable - or determining that many solutions exists - reduces to solving a system of linear equations. There are a number of techniques to do this, with Gaussian elimination with backpropagation being a particularly common way to do this (though often you'd just hand this to MATLAB or a linear algebra package and have it do the work for you.)
There's no guarantee in general that given a collection of equations the computer can determine whether or not they have a solution or to deduce a value of a variable, but this happens to be one of the nice cases where the shape of the contraints make the problem amenable to exact solutions.
Alright I have figured some things out. To answer the question as per title directly, it's possible to represent average value in programming. 1 possible way is to create a list of map data structures which store the set collection as key (eg. "a,b,c"), while the average value of the set will be the value (eg. 25).
Extract the key and split its string by comma, store into list, then multiply the average value by the size of list to get the total (eg. 25x3 and 23x2). With this, no semantic information will be lost.
As for the context to which I asked this question, the more proper description to the problem is "Given a set of average values of different combinations of variables, is it possible to find the value of each variable?" The answer to this is open. I can't figure it out, but below is an attempt in describing the logic flow if one were to code it out:
Match the lists (from Paragraph 2) against one another in all possible combinations to check if a list contains all elements in another list. If so, substract the lists (eg. abc-bc) as well as the value (eg. 75-46). If upon substracting we only have 1 variable in the collection, then we have found the value for this variable.
If there's still more than 1 variables left such as abcd - bc = ad, then store the values as a map data structure and repeat the process, till the point where the substraction count in the full iteration is 0 for all possible combinations (eg. ac can't substract bc). This is unfortunately not where it ends.
Further solutions may be found by combining the lists (eg. ac + bd = abcd) to get more possible ways to subtract and derive at the answer. When this is the case, you just don't know when to stop trying, and the list of combinations will get exponential. Maybe someone with strong related mathematical theories may be able to prove that upon a certain number of iteration, further additions are useless and hence should stop. Heck, it may even be possible that negative values are also helpful, and hence contradict what I said earlier about 'ac' can't subtract 'bd' (to get a,c,-b,-d). This will give even more combinations to compute.
People with stronger computing science foundations may try what templatetypedef has suggested.

Calculate derivative of an array with apache-commons-math

Good Morning,
I have an array with about 3000 double values, I need to find all local minimum and maximum, for this I'm interested to first and second derivative, what's best way to achieve this with Apache Commons Math? My trouble is that I'm starting directly from the array, not from a function like sin(x).
Thanks
With just an array you wont be able to find a min/max.
If the array was calcualted from a known function, then you could differentiate it numerically (just calculate at X and X + epsilon, and divide by epsilon, assuming that there's a single parameter that you're differentating with respect to).
Alternatively, is the array actually the list of coefficients of a big polynomial? If so, then the same approach might work.

Cant enter imaginary numbers in SageMathCloud

I am trying to simply enter the character "i" into SageMathCloud so that I can construct imaginary numbers, this is my syntax:
ran40 = matrix(QQ,2,2,[[2*i,-2],[3,4]])
show(ran40)
I am constructing a matrix so that I can compute the reduced-row echelon form. The first element that is 2*i just needs to be 2i but I keeping getting syntax errors, how do I enter i for an imaginary number into SageMathCloud? I cannot find an example anywhere on the internet, if anyone knows this syntax please do inform me.
Your problem is that you decided what the ring is, and that ring doesn't have any imaginaries.
matrix(QQ,2,2,[[2*i,-2],[3,4]])
QQ is the rationals (the fraction field of ZZ, if you like).
Is this what you want? It will make things in the "symbolic ring" which can be slow...
M = matrix(2,2,[[2*i,-2],[3,4]])
show(M)
show(M.echelon_form())
show(M.rref())

Quadratic form in R

This is probably straightforward, elementary and whatever, but I can't manage to get it. I have 2 Nx1vectors u and w, which are composed by both negative and positive values. I am trying to compute w'u u'w , which should be a quadratic form. I should be able to write this like
t(w)%*%u%*%t(u)%*%w
However sometimes I get a negative value, depending on the values in the two vectors. This is not possible, since that thing is a quadratic form. I tried with
crossprod(w, u)%*%crossprod(u, w)
and
crossprod(w, u)*crossprod(u, w)
which gives positive and equal results. However, since I am dealing with Nx1vectors, I should also be able to write it as
`sum(w*u)^2`
which gives a positive value but different from the ones above.
So I guess I am doing something wrong somewhere. So, the question is: how can I express w'u u'w which is valid for both vectors and matrices ?
EDIT: here a csv file with the original vectors to reproduce exactly the same issue

proof NP-complete

Hi guys I have a question. I am wondering if anyone know how to proof it.
Here is the question:
The Subset Sum problem is shown to be NP-complete. The input is a sequence of positive numbers w1, ... ,wn, W, where W is the target weight. The problem is to decide whether there is a set of weights F ⊆ {1, ... ,n} such that the the sum of some weights equal to the target weight (i.e. w1 + ... + wi = W)
Let the Restricted Subset Sum problem be defined like Subset Sum, but with the extra requirement that the target weight is less than half the sum of all weights. (If this fails then the input must be rejected right away.) Show that Restricted Subset Sum is NP-complete.
Thank you.
You have to show (a) your problem is in NP and (b) your problem is NP hard. For (a), show that a solution to some problem in NP would make solving your problem easy (if you think about it, showing this is trivial). For (b), you need to show that a solution to your problem would make solving any problem in NP easy (in other words, find another NP-complete problem whose solution can be rephrased in terms of a solution to your problem).
This is already practically half the proof - (a) is trivial now - I'd prefer not to do the rest.

Resources