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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:55:39+00:00 2026-05-27T04:55:39+00:00

I wrote the following program in Python 2 to do Newton’s method computations for

  • 0

I wrote the following program in Python 2 to do Newton’s method computations for my math problem set, and while it works perfectly, for reasons unbeknownst to me, when I initially load it in ipython with %run -i NewtonsMethodMultivariate.py, the Python 3 division is not imported. I know this because after I load my Python program, entering x**(3/4) gives “1”. After manually importing the new division, then x**(3/4) remains x**(3/4), as expected. Why is this?

# coding: utf-8
from __future__ import division
from sympy import symbols, Matrix, zeros

x, y = symbols('x y')
X = Matrix([[x],[y]])
tol = 1e-3

def roots(h,a):
  def F(s):
    return h.subs({x: s[0,0], y: s[1,0]})
  def D(s):
    return h.jacobian(X).subs({x: s[0,0], y: s[1,0]})
  if F(a) == zeros((2,1)):
    return a
  else:
    while (F(a)).norm() > tol:
      a = a - ((D(a))**(-1))*F(a)
      print a.evalf(10)

I would use Python 3 to avoid this issue, but my Linux distribution only ships SymPy for Python 2. Thanks to the help anyone can provide.

Also, in case anyone was wondering, I haven’t yet generalized this script for nxn Jacobians, and only had to deal with 2×2 in my problem set. Additionally, I’m slicing the 2×2 zero matrix instead of using the command zeros(2,1) because SymPy 0.7.1, installed on my machine, complains that “zeros() takes exactly one argument”, though the wiki suggests otherwise. Maybe this command is only for the git version. (Thanks eryksun for correcting my notation, which fixed the issue with the zeros function.)

  • 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-27T04:55:39+00:00Added an answer on May 27, 2026 at 4:55 am

    Both ipython -i command and run -i in ipython interpreter ignore from __future__ import division in print05.py script.

    $ cat print05.py 
    from __future__ import division
    print(1/2)
    

    In ipython console:

    In [1]: print 1/2
    0
    In [2]: run -i print05.py
    0.5
    In [3]: division
    Out[3]: _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
    In [4]: print 1/2
    0
    In [5]: from __future__ import division
    In [6]: print 1/2
    0.5
    

    execfile and import produce the same result:

    >>> print 1/2
    0
    >>> execfile('print05.py')
    0.5
    >>> print 1/2
    0
    >>> from __future__ import division
    >>> print 1/2
    0.5
    

    from __future__ import division should not have effect on the source code from different modules, otherwise it would break code in other modules that don’t expect its presence.

    Here, from __future__ import division has effect:

    $ python -i print05.py
    0.5
    >>> print 1/2
    0.5
    >>> division
    _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
    

    The module name in this case is __main__ both inside print05.py and in the prompt.

    Here, the first print 1/2 executes in print05 module, the second one in __main__ module so it also works as expected:

    $ python -im print05
    0.5
    >>> print 1/2
    0
    

    And here’s something wrong:

    $ ipython -i print05.py
    0.5
    In [1]: division
    Out[1]: _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
    In [2]: print 1/2
    0
    

    The docs for __future__ say:

    If an interpreter is started with the -i option, is passed a script
    name to execute, and the script includes a future statement, it will
    be in effect in the interactive session started after the script is
    executed.

    So It might be a bug in ipython if its -i option tries to emulate the same python option.

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

Sidebar

Related Questions

I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
In Visual C++ i wrote the following sample in a C++ program: float f1
I wrote a CPU monitoring program in Python. For some reason sometimes the the
I have a Python program running a thread that consistently outputs the following: (my_program.py:12313):
To launch programs from my Python-scripts, I'm using the following method: def execute(command): process
The following (a small C program and a python script that calls it) behave
Just wrote my first python program! I get zip files as attachment in mail
I am using ImageMagick library with Python ctypes . I wrote a following simple
When I write the following program: file 1: #include <stdio.h> int global; void print_global1()
The following line in my C program should provided All/Group/Owner read and write permissions

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.