3

I'm looking to build a numpy array as follows so that I don't have to hard code countless numpy arrays by hand:


def func_1(x):
 return x**2

def func_2(x):
 return x**3+1

So the array becomes:

     | func_1(x) func_1(x) func_2(x) |
     | func_1(x) func_1(x) func_2(x) |
A =  | func_1(x) func_1(x) func_2(x) |
     | func_1(x) func_1(x) func_2(x) |

now with this array filled with functions for each element create many versions of A:

         | 1  1  2 |
         | 1  1  2 |
A(x=1) = | 1  1  2 |
         | 1  1  2 |


         | 4  4  9 |
         | 4  4  9 |
A(x=2) = | 4  4  9 |
         | 4  4  9 |

Update

I implemented this as follows:

    def h(x):
        return np.exp(-((x - 1)**2/ (2*(0.25**2))))

    def l(x):
        return np.exp(-((x - 0)**2/ (2*(0.25**2))))

    def m(x):
        return np.exp(-((x - 0.5)**2/ (2*(0.25**2))))

    def fuzzy_patterns(x):
        return np.array([ 
           # pattern_1
           np.array ([
               [l(x), l(x), h(x)],
               [l(x), l(x), h(x)],
               [l(x), l(x), h(x)]
           ]),

           # pattern_2
           np.array ([
               [h(x), h(x), l(x)],
               [h(x), h(x), l(x)],
               [h(x), h(x), l(x)]
           ]),

           # pattern_3
           np.array ([
               [h(x), h(x), h(x)],
               [l(x), l(x), l(x)],
               [l(x), l(x), l(x)]
           ]),
           .
           .
           .,
            # pattern_n
           np.array ([
               [m(x), m(x), m(x)],
               [m(x), l(x), m(x)],
               [m(x), m(x), m(x)]
           ]),


In the end, this seemed like the most straightforward way to go considering the readability of code. I'll accept hiro protagonist's answer as my implementation is most similar to their answer.

4
  • It looks like you actually want A to be a function that returns an array. Just write a function that returns an array of the form you showed. Commented Aug 29, 2019 at 6:55
  • You could make an array with object dtype that contains the unevaluated functions. But there isn't a way of evaluating such an array. You'd have to iterate over the array, doing the evaluation element by element. np.frompyfunc is a handy tool for working with object arrays, element by element. Commented Aug 29, 2019 at 7:21
  • @BrenBarn "A" is just an example. If I implement all the types arrays I want to make I'll need to create many functions which can become quite hairy and confusing as some arrays may have every element made from a different math function. Commented Aug 29, 2019 at 7:31
  • Looks like an XY problem here. You are asking how to fill a numpy array with functions, but complain that the proposed solutions/workarounds require you to code many functions by hand. If the actual question "how to dynamically generate functions from a certain algorithm", ask it directly. (If so, the answer probably involves functools.partial ) Commented Aug 29, 2019 at 14:31

1 Answer 1

1

this reproduces what you want:

def A(x):
    a = np.full(shape=(3, 2), fill_value=func_1(x))
    b = np.full(shape=(3, 1), fill_value=func_2(x))
    return np.concatenate((a, b), axis=1)

i concatenate the 2 constant arrays (np.full) to the result.

you may want to add dtype=int to np.full if you want your arrays to be integer-valued.


if your functions depend on the coordinates there is numpy.fromfunction.

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

1 Comment

This would be kinda inefficient to implement as I would have a number of functions to implement. Plus there are some arrays that would only require the central element or the corner elements to be made from different functions.

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.