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 6614007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:17:13+00:00 2026-05-25T20:17:13+00:00

Given log(a) and log(b) , I want to compute log(a+b) (in a numerically stable

  • 0

Given log(a) and log(b), I want to compute log(a+b) (in a numerically stable way).

I wrote a little function for this:

def log_add(logA,logB):
    if logA == log(0):
        return logB
    if logA<logB:
        return log_add(logB,logA)
    return log( 1 + math.exp(logB-logA) ) + logA

I wrote a program where this is by far the most time-consuming piece of code. Obviously I could try to optimize it (eliminate the recursive call, for instance).

Do you know of a standard math or numpy function for computing log(a+b) from log(a) and log(b)?

If not, do you know of a simple way to make a single C++ hook for this function? It’s not a complicated function (it uses floats), and as I said, it’s taking up the majority of my runtime.

Thanks in advance, numerical methods ninja!

  • 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-25T20:17:14+00:00Added an answer on May 25, 2026 at 8:17 pm

    Note: Best answer until now is to simply use numpy.logaddexp(logA,logB).

    Why exactly do you compare with log(0)? This is equal to -numpy.inf, in this case you come to log(1 + math.exp(-inf-logB) ) + logB Which reduces itself to logB. This call always will give an warning message which is extremely slow.

    I could come up with this one-liner. However you’ll need to really measure to see if this is actually faster. It does only use one ‘complex’ calculation function instead of the two that you use, and no recursion is happening, the if is still there but hidden (and maybe optimized) in fabs/maximum.

    def log_add(logA,logB):
        return numpy.logaddexp(0,-numpy.fabs(logB-logA)) + numpy.maximum(logA,logB)
    

    edit:

    I did a quick timeit() with following results :

    1. Your original version took about 120s
    2. My version took about 30s
    3. I removed the compare with log(0) from your version and it came down to 20s
    4. I edited my code to keep the logaddexp but also worked with your recursive if and it went down to 18s.

    Updated code, you could also switch the recursive call with an inline updated formula but this made little difference in my timing tests:

    def log_add2(logA, logB):
        if logA < logB:
            return log_add2(logB, logA)
        return numpy.logaddexp(0,logB-logA)+logA
    

    Edit 2:

    As pv noted in comments, you could actually just do numpy.logaddexp(logA, logB) which comes down to calculating log(exp(logA)+exp(logB)) which is of course equal to log(A+B). I timed it (on the same machine as above) and it went further down to about 10s. So we’ve come down to about 1/12, not bad ;).

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

Sidebar

Related Questions

Given this var: somevar = {dothis: function(){console('yay')}}; If I want to hijack it, I
Given a (source) patch file, what's the easiest way to apply this patch on
Specifically I want O(log n) insertion/deletion times and O(1) operation for find_next_higher_element which given
Say I'm given n=32. I want to know what log_2(n) is. In this case,
I want to send log files to a given email address. Here is how
I want to encrypt a given data using the log-in user's password on a
Given this class: class Foo { readonly ILog log; public Foo(ILog log) { this.log
function saveCallerReference(callerReference){ $.getJSON('/index.php?r=site/AJAXsaveCallerReference', function(data) { console.log(data); return data; }); } Given the above, the
Given an object like this: { name: joe } I want to get the
given a traceback error log, i don't always know how to catch a particular

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.