Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7595169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:36:22+00:00 2026-05-30T21:36:22+00:00

I am building a class for constructing a certain type of approximating function (mathematical

  • 0

I am building a class for constructing a certain type of approximating function (mathematical function). This approximating function will be a linear combination of a given number of basis functions, which I store in a list, and it will return a scalar. A method of the class needs to update my approximating function in each iteration, by multiplying each basis function with a scalar (different scalars for different basis functions), and forming the sum. In other words, I need to update my approximating function by forming a linear combination of the basis functions and setting my approximating function to this unevaluated sum of functions.

The default basis for this class is a polynomial basis, which I’ve implemented with NumPy’s poly1d class. The way I form the linear combination is currently

sum(self.basis[i]*self.coeffs[i] for i in range(self.dim))

where (obviously) self.basis is the list of basis functions, self.coeffs is the list of scalars to multiply with the basis functions, and self.dim is the number of basis functions. This works for the poly1d class, but not, for example, if I use a basis like:

basis1 = [lambda x: float(x), lambda y: float(y)**2]

or

def x(y):
    return y
def x2(y):
    return y**2
basis2 = [x, x2]

Basis1 gives me the error

TypeError: only length-1 arrays can be converted to Python scalars

Basis2 gives me the error

TypeError: unsupported operand type(s) for *: 'function' and 'numpy.float64'

Likewise, using

sum(itertools.imap(operator.mul, self.basis, self.lambdas))

works for the poly1d basis, but not for the other bases above, and gives the same error messages.

So my question is: how do I form a linear combination of unevaluated functions in Python (using or not using NumPy)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:36:24+00:00Added an answer on May 30, 2026 at 9:36 pm

    A quick way to combine the function basis with the coefficients is a python dictionary:

     f = lambda x : x+1
     g = lambda x : 2*x
     d = { f : 7, g : 3 }
     print(d)
     d[f]=8
     print(d)
     def app (d,x): return  np.sum([ v * k(x) for k,v in d.items() ] )
     print(app(d,0))
    

    This prints:

    {<function <lambda> at 0x115e7dc08>: 7, <function <lambda> at 0x115e7de60>: 3}
    {<function <lambda> at 0x115e7dc08>: 8, <function <lambda> at 0x115e7de60>: 3}
    8
    

    Then you will need to derive a Dict subclass to handle the mathematical operations that you need.

    Even simpler would be to keep the coef and function lists separate and operate just on the coef list until you need to evaluate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a class library that will have some public & private methods. I
I'm building little class called Output ... It's meant to be something that will
I'm using prototype 1.7 and building a class that will essentially take a list
So I'm building a class, for simplicity, I'll dumb it down here. This gives
Given a DIV with this general structure (class=post, from an extracted message board post)
So this is the thing, Im building dispatcher class that needs to dispatch http
I am building a class library, and I am getting an error and I
I am building a class which subclasses dict , and overrides __setitem__ . I
When I'm building a class library I usually create a file Enums.cs to hold
I am building DB class, in the constructor I want to establish the connection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.