0

I have a file in which I need to use the first column. The remaining columns need to be integrated with respect to the first. Lets say my file looks like this:

100 1.0 1.1 1.2 1.3 0.9
110 1.8 1.9 2.0 2.1 2.2
120 1.8 1.9 2.0 2.1 2.2
130 2.0 2.1 2.3 2.4 2.5

Could I write a piece of code that takes the second column and integrates with the first then the third and integrates with respect to the first and so on? For my code I have:

import scipy as sp
first_col=dat[:,0] #first column from data file
cols=dat[:,1:] #other columns from data file

col2 = cols[:,0]  # gets the first column from variable cols
I = sp.integrate.cumtrapz(col2, first_col, initial = 0) #integration step

This works only for the first row from the variable col, however, I don't want to write this out for all the other columns, it would look discussing (the thought of it makes me shiver). I have seen similar questions but haven't been able to relate the answers to mine and the ones that are more or less the same have vague answers. Any ideas?

skitt1z1
  • 85
  • 1
  • 2
  • 7
  • you can use `genfromtxt` and then perform all the integration yourself as an alternative – Matthew May 05 '15 at 12:49
  • What parameters would allow me to cycle through the columns? – skitt1z1 May 05 '15 at 13:04
  • 1
    if ive understood what you want right, you want to apply the same integration over several columns, so `data = sp.genfromtxt("file.txt") ; for col in data[0]: ; integrate(data[:,0], data[:,col])` where `integrate` is a def function. (though the answer seems to do what you want without having to write it yourself!) – Matthew May 05 '15 at 13:19

1 Answers1

1

The function cumtrapz accepts an axis argument. For example, suppose you put your first column in x and the remaining columns in y, and they have these values:

In [61]: x
Out[61]: array([100, 110, 120, 130])

In [62]: y
Out[62]: 
array([[ 1.1,  2.1,  2. ,  1.1,  1.1],
       [ 2. ,  2.1,  1. ,  1.2,  2.1],
       [ 1.2,  1. ,  1.1,  1. ,  1.2],
       [ 2. ,  1.1,  1.2,  2. ,  1.2]])

You can integrate each column of y with respect to x as follows:

In [63]: cumtrapz(y, x=x, axis=0, initial=0)
Out[63]: 
array([[  0. ,   0. ,   0. ,   0. ,   0. ],
       [ 15.5,  21. ,  15. ,  11.5,  16. ],
       [ 31.5,  36.5,  25.5,  22.5,  32.5],
       [ 47.5,  47. ,  37. ,  37.5,  44.5]])
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214