0

I have the following np.array which describes a rectangular wave. I would like to transform it into a callable function with a continuous argument. The np.array is:

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import seed
from numpy.random import rand


def piecewise_control( off_times,amp_inj, period_inj ):

   def select(T):
       
       return lambda t: (-T/2 <= t) & (t < T/2)

   def pulse_train(t, at, shape):
       
       return np.sum(shape(t - at[:,np.newaxis]), axis=0)



   for i in range(1,len(off_times)):

     off_times[i] += off_times[i-1]  + period_inj

 
  
   return  amp_inj*pulse_train(t,off_times,shape=select(period_inj))               


t=np.linspace(0,100,10000)
off_times = 10*rand(10)
period_inj = 1
amp_inj = 1 

control = piecewise_control(  off_times,amp_inj, period_inj )

plt.plot(t,control)
plt.show()

This answer inspired me.

The plot is the following:

enter image description here

The question is: can we transform the array control into a function with a continuous argument?

Of course if we did:

def ccontrol(t, control):
      return control[t]

unfortunately we would get a function which only depends on integers.

4
  • 2
    Which floating variable do you mean? Commented Apr 30, 2021 at 1:24
  • @Tarik sorry I removed that word because it was misleading. I want something callable just as a function like sint(t). Commented Apr 30, 2021 at 10:09
  • 1
    Which array do you want to transform into a function? Can you provide the function signature (arguments) and the expected return? Commented Apr 30, 2021 at 21:06
  • @Tarik the array is control which is defined via the function piecewise_control. I would like to have this array just as a callable function defined for each argument. I thought that an interpolation technique or a dense output would have been a key point. Commented May 2, 2021 at 13:47

1 Answer 1

3

You can subclass numpy.ndarray and implement the __call__ method:

import numpy as np

class MyArray(np.ndarray):
    def __call__(self, idx):
        return self[idx]

control = np.random.rand(100)
control_view = control.view(MyArray)

print(control_view(5), control[5])

For interpolation you can use scipy.interpolate. In fact, interpolation routines can return functions that you can call with any input, not necessarily integers.

Sign up to request clarification or add additional context in comments.

3 Comments

Your solution gives the same output of my ccontrol function.
@Siderius, right, but it's a callable array, as opposed to a separate function. Now that you edited the question saying you need to call this function with floating-point numbers, scipy.interpolate does exactly that.
wrapping the index with int(): return self[int(idx)] would do the trick here. (zero-order interpolation)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.