Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78518

Re: how to parse standard algebraic notation

References <4a7f3e7a-ba4d-405e-a078-937bf861fcd8@googlegroups.com>
Date 2014-10-01 10:20 +1000
Subject Re: how to parse standard algebraic notation
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.14502.1412122860.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Oct 1, 2014 at 6:53 AM, math math <mathematisch@gmail.com> wrote:
> I want to basically write a program that inputs a polynomial in standard algebraic notation and outputs its derivative.
>
> I know that I need to get the exponent somehow, but I am not sure how to accomplish this in python (3.3)

As Gary says, the form of input is really the critical part. How will
users type these?

One simple cheat you could do would be to prompt for each term
separately - something like this:

exponent = int(input("What is the highest exponent used? "))
terms = []
while exponent:
    terms.append(input("Enter coefficient of x^%d: " % exponent))
    exponent -= 1
terms.append(input("Enter constant term: "))

You could do something similar with a GUI toolkit like tkinter or
pygtk, too; provide input fields for the coefficients, and labels in
between that say "x² + " etc.

Otherwise, you're going to be primarily bound by the keyboard - your
program is useless if it takes longer to key data into it than to do
the work manually. Python 3.3 has excellent Unicode support, and
Unicode has excellent support for mathematical notations, so you
should be able to produce nice-looking output; but mathematical
notation is much easier to write on a blackboard than on a computer's
keyboard. (And even though some keyboards are black, like the one I'm
using right now, it doesn't make it any easier. Sorry.) Gary's
suggestions of the double asterisk and caret are two commonly-used
notations for exponentiation in computers, so either would make some
sense. Otherwise, you could come up with some other notation, but
mainly, it has to be typeable.

But hey! Once you have something you can type unambiguously, we can
help you parse that into something you can make use of! That part's
not too hard.

ChrisA

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

how to parse standard algebraic notation math math <mathematisch@gmail.com> - 2014-09-30 13:53 -0700
  Re: how to parse standard algebraic notation Gary Herron <gherron@digipen.edu> - 2014-09-30 14:05 -0700
    Re: how to parse standard algebraic notation math math <mathematisch@gmail.com> - 2014-10-01 00:01 -0700
      Re: how to parse standard algebraic notation Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-01 08:35 +0100
      Re: how to parse standard algebraic notation Chris Angelico <rosuav@gmail.com> - 2014-10-01 18:38 +1000
        Re: how to parse standard algebraic notation math math <mathematisch@gmail.com> - 2014-10-01 05:04 -0700
      Re: how to parse standard algebraic notation Marco Buttu <marco.buttu@gmail.com> - 2014-10-01 14:22 +0200
      Re: how to parse standard algebraic notation Marco Buttu <marco.buttu@gmail.com> - 2014-10-01 14:26 +0200
  Re: how to parse standard algebraic notation Chris Angelico <rosuav@gmail.com> - 2014-10-01 10:20 +1000
  Re: how to parse standard algebraic notation wxjmfauth@gmail.com - 2014-10-01 01:08 -0700
  Re: how to parse standard algebraic notation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-02 02:45 +1000
    Re: how to parse standard algebraic notation Chris Angelico <rosuav@gmail.com> - 2014-10-02 03:01 +1000

csiph-web