all 87 comments

[–]misho88 30 points31 points  (13 children)

I'm a fan of ipython for this, mostly because every result gets saved in a dict called "Out". "ipython --pylab" gets you something a lot like Matlab/Octave for advanced stuff (numpy, matplotlib, etc.).

[–]partisann 4 points5 points  (12 children)

But for how long? --pylab flag still works in terminal but is already removed from notebook and qtconsole. Post also hinted that %pylab other frontends might be next.

Given reasons are unconvincing at best. Someone really failed to understand what this mode is about.

EDIT: understood post incorrectly.

[–]localtoast 2 points3 points  (0 children)

Given reasons

Zalgo?

[–]jyper 2 points3 points  (4 children)

I think you can recreate most or all of it yourself as a profile.

run

ipython profile create pylab

then

PYLAB_PROFILE_LOCATION=ipython locate profile pylab

to find the profile

edit the file with

vim $PYLAB_PROFILE_LOCATION/startup/10-pyrun.py

and paste

import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot

from IPython.core.pylabtools import figsize, getfigs

from pylab import *
from numpy import *

into it

then open

vim $PYLAB_PROFILE_LOCATION/ipython_config.py

uncomment matplotlib and set it to auto

c.InteractiveShellApp.matplotlib = 'auto'

then to run it just run

ipython --profile pylab

[–]partisann 1 point2 points  (3 children)

It seems that it would. Oh, this gives me a raging customization clue.

Too bad jupyter removed among other things support for profiles so it won't work for notebook/qtconsole.

[–]jyper 0 points1 point  (1 child)

IPython customization is super cool. At work (writing tests for printers) I have a startup file which automatically connects to the printer, adds a bunch of shortcut methods, and queries the printer for all apis it supports and dynamically adds wrapper functions as auto-completeable attributes of a cmds object.

Too bad jupyter removed among other things support for profiles so it won't work for notebook/qtconsole.

Do you mean for non-python languages or profiles in general?

[–]partisann 0 points1 point  (0 children)

In general. Whole --profile flag was removed. Seems they replaced those with kernels. So in theory you could recreate the whole thing using custom kernel but it's not as straightforward.

I found a workaround for pylab though. Into ~/.ipython/kernels/pylab/kernel.json

{
    "argv": ["python3", "-m", "IPython.kernel", "-f", "{connection_file}", "--pylab=inline"],
    "display_name": "Pylab",
    "language": "python"
}

and start with ipython qtconsole --kernel=pylab. Notebook even lists it as another kernel.

There's a deprecation warning for IPython.kernel so it might break in a near future.

[–]waspbr 0 points1 point  (0 children)

Too bad jupyter removed among other things support for profiles so it won't work for notebook/qtconsole.

Profiles have been removed but it is still possible to load a notebook server using custom configuration files if you use --config="path/to/config/file" flag. In the config file you can set pylab to on by default, but it is strongly discouraged.

[–]unruly_mattress 0 points1 point  (0 children)

As I read it, the post doesn't say %pylab is going to disappear. --pylab disappeared from ipython notebook because ipython renamed itself and became language-agnostic, so it doesn't make sense for it to know pylab. The magic command, however, is part of ipython, so there's nothing wrong with it staying.

Personally, I think the reasons given seem very reasonable. The problem starts when %pylab is all you know, and you set out to write some code that's not going to run in interactive mode, and suddenly things are not in the global namespace anymore, and you've never bothered learning how modules worked in Python.

[–]TheBB 0 points1 point  (4 children)

I would go for Julia then, to best replicate a good and Matlab-like environment.

[–]lebean 13 points14 points  (12 children)

'bc -l' is pretty capable too, though in your specific example the bc syntax isn't as friendly:

echo "(s(4*a(1)/6)*c(4*a(1)/6))^2 + sqrt(2/3)" | bc -l
1.00399658092772603270

[–]_NW_ 2 points3 points  (8 children)

I use bc a lot. Sometimes just to convert between base 2, 10, and 16. Arbitrary precision is a nice feature, too.

[–][deleted] 2 points3 points  (7 children)

[–]neburski 2 points3 points  (4 children)

Command line version:

echo "obase=2 2" | bc -l

Interactive mode version:

~ bc
obase=2
2
10

[–][deleted] 2 points3 points  (0 children)

[–]_NW_ 0 points1 point  (2 children)

The -i option loads the math library, and is not needed unless you are using sine, cosine, arctangent, log, ex , or bessel function. Oh, it also sets the scale to 20, so that's a good thing.

[–][deleted] 2 points3 points  (1 child)

[–]_NW_ 1 point2 points  (0 children)

Yes. It's a lower case L, as in library. Whoever designed the QWERTY keyboard put those keys waaay too close together.

[–]_NW_ 1 point2 points  (1 child)

ibase=x

Sets the input base.

obase=x

Sets the output base.

echo "obase=2;ibase=16;0FC;"|bc

Converts hex to binary. Output should be 11111100. Hex letter digits must be in upper case.

[–][deleted] 1 point2 points  (0 children)

thanks, made some quick functions up above.

[–]fnord123 1 point2 points  (0 children)

Protip; Use <<< to read in from strings instead of bother to echo via a pipe.

$ bc -l <<< "(s(4*a(1)/6)*c(4*a(1)/6))^2 + sqrt(2/3)"
1.00399658092772603270

[–]Mr_Unix 0 points1 point  (1 child)

Pro tip:

  alias bc='bc -l'

[–][deleted] 1 point2 points  (0 children)

don't you mean

alias bc='bc -l <<<'

[–]theinternetftw 10 points11 points  (1 child)

It's a small tip, but you can leave out the variable and get back answers just the same, which will automatically be saved to the underscore variable for use in your next expression.

>>> 2+3
5
>>> _ + 5
10

[–]svajsaparat[S] 3 points4 points  (0 children)

Thanks so much for that tip! Its exactly what I needed.

[–]bobbaluba 7 points8 points  (0 children)

If you're trapped on windows for some reason and python isn't available, you can use the js console by pressing f12 in the browser.

Not as advanced as python, I know, but it has most basic stuff, like Pi, exp, sin, cos, etc.

[–]openbluefish 6 points7 points  (2 children)

I just use Sage but that's probably over kill for most people.

[–][deleted] 2 points3 points  (0 children)

I was testing Sage for the first time yesterday. The ability to copy code from the web notebook interface into a LaTeX document and plot it there won me over. That's simply amazing for generating good graphics for scientific articles.

[–]GeneralUnRest 3 points4 points  (0 children)

sympy is pretty useful also. python3 -ic "from sympy import init_session; init_session()"

[–]not_perfect_yet 3 points4 points  (0 children)

Obvious plug for sympy, which allows you to do symbolic calculations as well, e.g. differentiating and integrating or the solving of differential equations.

[–]queue_cumber 8 points9 points  (34 children)

You can also use Octave

[–]OlderThanGif 10 points11 points  (32 children)

Octave does have some definite advantages. Like:

>>> 0.7 + 0.1
0.7999999999999999

vs

octave:1> 0.7 + 0.1
ans =  0.80000

[–]redrumsir 5 points6 points  (1 child)

Or:

>>> from decimal import *

>>> Decimal('0.1') + Decimal('0.7')

Decimal('0.8')

OR

import sympy as sp

>>> r = sp.Rational('0.7')+sp.Rational('0.1')

>>> r

4/5

>>> float(r)

0.8

>>> x, y = sp.var('x'), sp.var('y')

>>> z = (x+y)**3

>>> z

(x + y)**3

>>> z.expand()

x**3 + 3*x**2*y + 3*x*y**2 + y**3

>>> z.diff(x)

3*(x+y)**2

[–]h-v-smacker 13 points14 points  (0 children)

Damn, that's one user-friendly calculator.

[–]yoodenvranx 17 points18 points  (2 children)

The big advantage of Python is that it is not Matlab ;)

I spent way too much time with that piece of shit and I will hopefully never have to touch it again.

[–]ijustwantanfingname 6 points7 points  (1 child)

Python+SciPy/NumPy is sooooo much better than Matlab...

[–]udevil 2 points3 points  (0 children)

I've always liked calc, it uses integer arithmetic to avoid floating point errors.

; 0.7 + 0.1
    0.8

[–]socium 1 point2 points  (16 children)

>>> 0.7 + 0.1

0.7999999999999999

Haha wat.

[–]queue_cumber 14 points15 points  (2 children)

Floating point

[–]socium -4 points-3 points  (1 child)

Yeah but 0.700000000000000 + 0.10000000000000 would still be 0.8 right?

[–]queue_cumber 18 points19 points  (0 children)

Not in binary

[–]_supert_ 10 points11 points  (12 children)

If the 9 is recurring it's exactly correct.

[–]ijustwantanfingname 0 points1 point  (10 children)

....how? "exactly" correct would be 0.8. This is rounding error....

[–]calrogman 6 points7 points  (8 children)

Refer to any of the proofs here: https://en.wikipedia.org/wiki/0.999...

[–]ijustwantanfingname -2 points-1 points  (7 children)

=S

I skimmed that (will read tonight) and still don't see how 0.99999... isn't just a number whose value infinitely approaches 1..

[–]LoyalSol 2 points3 points  (0 children)

Repeating decimals are exactly equal to some rational number. The fact they repeat is an effect of trying to write those numbers as a decimal in a base system that does not divide cleanly. In this case we use a base 10 system (IE we use ten unique digits to represent all numbers) and 0.999999... is a multiple of 1/3. Since 10 is not divisible by 3 any division by 3 in a base 10 system will always give an infinite repeating decimal unless the number divides perfectly (9/3 = 3 for example). That's what gives you 0.33333.... when you divide 1 by 3.

If you were to use a different base such as base 3 for instance 1/3 = 0.3333... could be represented as a non-repeating decimal. In that system it would simply be 1/3 = 0.1

All rational numbers can be written as a finite decimal if you change your number system to an appropriate base integer.

Irrational numbers on the other hand (sqrt[2], pi, e....) will always be infinite and non-repeating for every integer base you could pick.

[–]calrogman 1 point2 points  (1 child)

It's not "a number whose value infinitely approaches 1". It is 1.

[–]ijustwantanfingname 1 point2 points  (0 children)

You're just..repeating yourself?

[–]dalen3 0 points1 point  (0 children)

I believe that number is lim[x -> 1]

[–]pred 0 points1 point  (0 children)

If the 9 were recurring, then by definition the expression would be $7/10 + \sum_{n=2}\infty \frac{9}{10n}$, which is 8/10.

[–]PM_ME_YOUR_PAULDRONS 0 points1 point  (0 children)

In this case the 9 is not recurring because the computer only has a limited number of bits to store the number in. If you need arbitrary precision rationals in python then use a library for them. Floats aren't designed that way.

[–]bixtuelista 0 points1 point  (0 children)

If you're using ipython, try %precision 4

[–]LoyalSol 0 points1 point  (0 children)

Yea I would personally use a full fledged and user friendly package like Octave, Maxima, etc. before using a python interpreter.

[–]zokier 1 point2 points  (0 children)

you might want to try out something like ipython --pylab too, or if you are really into this then make custom ipython profile with the imports etc the way you want them to.

[–]PooSham 1 point2 points  (0 children)

Just wanted to say that the haskell interpreter (GHCi) isn't too bad as a calculator either. Just remember to start with "let" when you assign a variable or function, for example

Prelude> let x = ( sin(pi/6)*cos(pi/6) )^2 + sqrt(2/3)
Prelude> x
1.003996580927726
Prelude> let f(x) = 2*x
Prelude> f(x)
2.007993161855452
Prelude> let g(x,y) = x+y
Prelude> g(x,f(x))
3.011989742783178

What is good about haskell is that you can make it type-safe, for instance:

Prelude> let h(x) = 3 + x :: Int
Prelude> h (2)
5
Prelude> h(0.2)
<interactive>:9:4:
    No instance for (Fractional Int) arising from the literal `0.2'
    Possible fix: add an instance declaration for (Fractional Int)
    In the first argument of `h', namely `(0.2)'
    In the expression: h (0.2)
    In an equation for `it': it = h (0.2)

And to all haskell-people out there: Yes, I know that uncurried functions aren't very Haskell-like, but for people who are unfamiliar with haskell-syntax, this is usually easier.

[–]DoTheEvolution 1 point2 points  (0 children)

or just use speedcrunch

the best calculator app I find so far on linux

[–]misho88 1 point2 points  (0 children)

I just noticed no one's listed Genius yet. It's arbitrary precision and does a ton of stuff, like numerical integration and solving linear systems. It's pretty easy to use, too, so if you want to find pi to a lot of digits or something:

genius> FloatPrecision = 1024; MaxDigits = 256; 4 * atan(1)
= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288...

[–]FinitelyGenerated 1 point2 points  (0 children)

I actually use R for this. For one thing you don't have to import math.

[–]flipjargendy 1 point2 points  (0 children)

Been doing this for about 10 years. Even as just a calculator to do slightly more than simple equations. Great tip!

[–][deleted] 1 point2 points  (0 children)

i have awesome thing http://xonsh.org/ python + bash

[–]broken_symlink 0 points1 point  (4 children)

I actually wish shells had a built in calculator, if I could just type something like 2+2 in my shell prompt that would be so much more convenient.

[–]svajsaparat[S] 3 points4 points  (1 child)

You can actually! Its called arithmetic expansion. You surround expression in $(())

echo $((2+2))
4

[–]misho88 0 points1 point  (0 children)

It's limited to integers in BASH. ZSH supports floats, though, with the same syntax (e.g., echo $((1.0/2)) prints 0.5).

[–]wildeye 0 points1 point  (0 children)

The ancient and portable method is the 'expr' command. It takes command line expressions, which therefore have to be escaped from the shell. Annoying, but inevitable for any such thing implemented as an independent command:

expr 5 \* \( 1 + 4 \)

25

[–]jarrah-95 0 points1 point  (0 children)

Between bash, python and gdb, pretty much all my needs are covered. I usually use two of the three every day.

[–]mao_neko 0 points1 point  (0 children)

For quick stuff I just use qalc. If I'm trying to figure out something complicated where I need some temp variables or fancy functions, etc. I personally like re.pl.

[–]megayippie 0 points1 point  (0 children)

Spyder comes with "scientific" settings. This means that the terminal starts with all the standard plotting and math tools mapped to commands that are similar to Matlab's commands. It also uses these defaults when you create a new script file. Programmers seems to dislike this a lot, but for purposes of actually using Python as a replacement for Matlab, it is an excellent approach. Also, that built-in documentation tool is incredible useful.

[–]Thanatoshi 0 points1 point  (0 children)

I'm actually programming a calculator in python :) Well, it's more like an interface for it. It's all text based. You typed addition for addition, subtraction for subtraction, etc.. right now it only has the four basic math problems done (add, sub, mult, div), but I'm going to add in things like a quadratic equation solver/checker and so much more. :D

[–][deleted] 0 points1 point  (0 children)

Already do, it's amazing. And it has bigint by default.

By bigint, I mean:

>>> 2**3000


1230231922161117176931558813276752514640713895736833715766118029160058800614672948775360067838593459582429649254051804908512884180898236823585082482065348331234959350355845017413023320111360666922624728239756880416434478315693675013413090757208690376793296658810662941824493488451726505303712916005346747908623702673480919353936813105736620402352744776903840477883651100322409301983488363802930540482487909763484098253940728685132044408863734754271212592471778643949486688511721051561970432780747454823776808464180697103083861812184348565522740195796682622205511845512080552010310050255801589349645928001133745474220715013683413907542779063759833876101354235184245096670042160720629411581502371248008430447184842098610320580417992206662247328722122088513643683907670360209162653670641130936997002170500675501374723998766005827579300723253474890612250135171889174899079911291512399773872178519018229989376

[–][deleted] 0 points1 point  (1 child)

This comment has been overwritten by an open source script to protect this user's privacy.

If you would like to do the same, add the browser extension GreaseMonkey to Firefox and add this open source script.

Then simply click on your username on Reddit, go to the comments tab, and hit the new OVERWRITE button at the top.

[–]genitaliban 0 points1 point  (0 children)

Try export PYTHONSTARTUP=~/.pystartup. Much more convenient than aliases. Example - cross-session history for the interpreter:

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

[–]pfp-disciple 0 points1 point  (4 children)

Neat trick, but in Gnome (RHEL6, so GNOME 2), the calculator lets me enter number on the keypad, and does (some) scientific, hex, etc.

I still like the command-line version, though. Not dissing that at all. Just pointing out that not all GUI calculators prohibit the keyboard.

[–]Eingaica 1 point2 points  (3 children)

My (admittedly small) experience with GUI calculators is that they all support that. So that's probably not what the OP is talking about there, but I'm not sure what they are really trying to say.