-1

I am pulling values from a text file that has two columns. The text file looks exactly as shown below, where the first number in every row is the "time" value, and the second number is just a "number"

0,311.235  
1,0  
2,5316.36  
3,-4086.90  
4,-1739.985

I got the code figured out for the main part except for one thing. I still to check for equivalence of positives and negatives. Read below and follow the code:

workspace = 'C:\\Users\master\Desktop'

time= np.loadtxt(workspace +'\\project.txt', delimiter = ',', usecols = range(1))
num = np.loadtxt(workspace +'\\project.txt', delimiter = ',', usecols = range(1,2))

last_neg_num = 0
for i in range(len(num)-1, -1, -1):   
    if num[i] < 0:
        num[i] += 2*last_neg_num
        last_neg_num = num[i]

last_pos_num = 0
for i in range(len(num)-1, -1, -1):   
    if num[i] > 0:
        num[i] += 2*last_pos_num
        last_pos_num = num[i]

print num[0], num[1]

This is just an example text file. I could have many more or less time values and many more numbers associated with it, and they could be either negative or positive.

Now, here is I want to do with the content in the text file:

  1. Bring all the negative values and positive values at time=0.
  2. To complete step 1, I need to take the very last value of either positive or negative number, and move it on the left after multiplying with the constant of 2.

For ex: -1739.985 is at time=4, so I need to move that time=3, and when I move that to time=3, I multiply it with 2, giving me -3479.97. The value of -4086.90 is sitting at time=3 as well, thus I will need to add -3479.97+(-4086.90) = -7566.87. Now, we have one number at time=3. However, there are no more numbers on the negative side. But, it needs to go to time=0. For that I need to go through time=2 and time=1. To go from time=3 to time=2, I need to again multiply -7566.87 by 2 = -15133.74. Since, there is nothing to add at time=2 (adding 0), I need to move to time=1 by multiplying -15133.74*2 = -30267.48, and finally to time=0 giving me -60534.96.

Doing the same thing with the positives, I got 21576.675, which clearly in terms of magnitude is not equal to the final negative value at time=0. Thus, the code needs to tell that they are not equivalent.

My code doesn't multiply the answer by 2, if at any time=, there is no number either in negative or positive side. How to solve that? Now, obviously the positives are not equal as negatives for this particular problem. The values needed to be considered equal if one of the values is greater by 1 but smaller than or equal to 15. So, for ex: at time=0, if I have 350 positive, and 359 at negative, then they are equal because negative is +9 from positive.

I hope that make sense. I also need a visual that shows what is in the text file. Bar graph should do.

Master
  • 59
  • 1
  • 2
  • 10
  • 2
    Your question is at best unclear. You need to show the sample data. It would be better if you created your MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)) with simple hard-coded values for the arrays to be processed. Your code doesn't show the definition of `num` (you seem to load `number`), so that's confusing too. You don't show where the np class comes from or what it does, and a search of Python (at least Python 2.7) documentation doesn't find `loadtxt` anywhere. That's confusing too. (Your throwaway final paragraph is…arrogant?) – Jonathan Leffler Feb 02 '16 at 04:33
  • I think I am pretty clear because I explained the whole calculation, and got to the answer. As I said, the code works, but doesn't exactly use that 2 everywhere. Yes, that `number` was a typo. Sorry about that. The code works and runs fine. Arrogant ? I am just stating that I need that. I am not telling anyone to do it. – Master Feb 02 '16 at 05:31
  • The code does not work. I copied it into a fresh interpreter and it threw a bunch of errors even after I made the obvious corrections (e.g., `import numpy as np`) – Paul H Feb 02 '16 at 05:37
  • 1
    @Master: No, I'm sorry, but your question is not clear. You've not shown the input data. You've not provided an MCVE. – Jonathan Leffler Feb 02 '16 at 07:04
  • "Bar graph should do". I expect it would. Have you attempted anything yourself? – SiHa Feb 02 '16 at 10:36
  • @PaulH You are correct about not having that import numpy, but everything else is good, and it works for me. I am using spyder to run my code. Thanks! – Master Feb 02 '16 at 18:42
  • @SiHa As I said earlier to Jonathan, I am just stating that I need to do it. I am not telling anyone on here to do it. All I need help with is getting what I have in the example. I DID ATTEMPT THAT PART. – Master Feb 02 '16 at 18:43

1 Answers1

1

It seems like you want to calculate the weighted sum of num, where the weights are 2**time, so:

import numpy as np

workspace = 'C:\\Users\master\Desktop'
time, num = np.loadtxt(workspace + '\\project.txt', delimiter=',', unpack=True)

res = np.sum(num * 2**time)
ares = np.abs(res)

print ares >= 1 and ares <= 15

as for the graphs, use matplotlib:

import matplotlib.pyplot as plt

plt.bar(time, num)
plt.show
Jan Christoph Terasa
  • 5,781
  • 24
  • 34
  • The output I am getting is "False". I don't understand why that is happening. Also, I am positive that there needs to be a loop in the code, which you don't have. If you got my code to work, then I believe you were able to figure out the problem, and also took into consideration the example I gave to correct my code. Any further help will be appreciated! Thanks. – Master Feb 02 '16 at 18:37
  • The print is the result of the requirement you stated: "The values needed to be considered equal if one of the values is greater by 1 but smaller than or equal to 15". So, if they are "equal" according to your requirement, we print `True`, else `False`. – Jan Christoph Terasa Feb 03 '16 at 08:05