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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:13:41+00:00 2026-06-14T22:13:41+00:00

I’m working with Python/numpy/scipy to write a small ray tracer. Surfaces are modelled as

  • 0

I’m working with Python/numpy/scipy to write a small ray tracer. Surfaces are modelled as two-dimensional functions giving a height above a normal plane. I reduced the problem of finding the point of intersection between ray and surface to finding the root of a function with one variable. The functions are continuous and continuously differentiable.

Is there a way to do this more efficiently than simply looping over all the functions, using scipy root finders (and maybe using multiple processes)?

Edit: The functions are the difference between a linear function representing the ray and the surface function, constrained to a plane of intersection.

  • 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-06-14T22:13:42+00:00Added an answer on June 14, 2026 at 10:13 pm

    The following example shows calculating the roots for 1 million copies of the function x**(a+1) – b (all with different a and b) in parallel using the bisection method. Takes about ~12 seconds here.

    import numpy
    
    def F(x, a, b):
        return numpy.power(x, a+1.0) - b
    
    N = 1000000
    
    a = numpy.random.rand(N)
    b = numpy.random.rand(N)
    
    x0 = numpy.zeros(N)
    x1 = numpy.ones(N) * 1000.0
    
    max_step = 100
    for step in range(max_step):
        x_mid = (x0 + x1)/2.0
        F0 = F(x0, a, b)
        F1 = F(x1, a, b)
        F_mid = F(x_mid, a, b)
        x0 = numpy.where( numpy.sign(F_mid) == numpy.sign(F0), x_mid, x0 )
        x1 = numpy.where( numpy.sign(F_mid) == numpy.sign(F1), x_mid, x1 )
        error_max = numpy.amax(numpy.abs(x1 - x0))
        print "step=%d error max=%f" % (step, error_max)
        if error_max < 1e-6: break
    

    The basic idea is to simply run all the usual steps of a root finder in parallel on a vector of variables, using a function that can be evaluated on a vector of variables and equivalent vector(s) of parameters that define the individual component functions. Conditionals are replaced with a combination of masks and numpy.where(). This can continue until all roots have been found to the required precision, or alternately until enough roots have been found that it is worth to remove them from the problem and continue with a smaller problem that excludes those roots.

    The functions I chose to solve are arbitrary, but it helps if the functions are well-behaved; in this case all functions in the family are monotonic and have exactly one positive root. Additionally, for the bisection method we need guesses for the variable that give different signs of the function, and those happen to be quite easy to come up with here as well (the initial values of x0 and x1).

    The above code uses perhaps the simplest root finder (bisection), but the same technique could be easily applied to Newton-Raphson, Ridder’s, etc. The fewer conditionals there are in a root finding method, the better suited it is to this. However, you will have to reimplement any algorithm you want, there is no way to use an existing library root finder function directly.

    The above code snippet is written with clarity in mind, not speed. Avoiding the repetition of some calculations, in particular evaluating the function only once per iteration instead of 3 times, speeds this up to 9 seconds, as follows:

    ...
    F0 = F(x0, a, b)
    F1 = F(x1, a, b)
    
    max_step = 100
    for step in range(max_step):
        x_mid = (x0 + x1)/2.0
        F_mid = F(x_mid, a, b)
        mask0 = numpy.sign(F_mid) == numpy.sign(F0)
        mask1 = numpy.sign(F_mid) == numpy.sign(F1)
        x0 = numpy.where( mask0, x_mid, x0 )
        x1 = numpy.where( mask1, x_mid, x1 )
        F0 = numpy.where( mask0, F_mid, F0 )
        F1 = numpy.where( mask1, F_mid, F1 )
    ...
    

    For comparison, using scipy.bisect() to find one root at a time takes ~94 seconds:

    for i in range(N):
        x_root = scipy.optimize.bisect(lambda x: F(x, a[i], b[i]), x0[i], x1[i], xtol=1e-6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.