Ask Your Question

slelievre's profile - activity

2020-02-19 19:07:06 -0600 received badge  Nice Answer (source)
2020-01-25 16:49:59 -0600 received badge  Great Answer (source)
2020-01-17 05:18:37 -0600 received badge  Nice Answer (source)
2020-01-16 18:48:51 -0600 answered a question ask.sagemath.org Y2k20 bug

This could be reported to one or both of:

2020-01-16 18:21:56 -0600 answered a question quick beginner question

This follows the Python style guide, also known as "Python enhancement proposal 8" or "PEP8".

Its goal is to make things easier to read.

2020-01-16 18:19:11 -0600 edited question quick beginner question

I see in tutorials that spaces are sometimes used although it doesn't seem to affect the functionality.

For instance, instead of x^2+3*x, it would be written as x^2 + 3*x.

Is this simply for viewing convenience?

Is there anything about using spaces that a beginner should be aware of? (no programming exp)

2020-01-16 18:10:11 -0600 commented answer matrix base ring changes by a complex multiplication

To obtain the CDF version of I you can also use CDF.gen() or CDF.gen(0).

2020-01-16 18:05:54 -0600 commented question New SageMath 9.0 installation not running on Catalina. SageMath 8.7 worked fine.

Could it be that Sage 8.7 was installed under macOS 10.14 Mojave and kept working after a system upgrade to macOS 10.15 Catalina?

2020-01-16 18:05:45 -0600 answered a question New SageMath 9.0 installation not running on Catalina. SageMath 8.7 worked fine.

The problem is probably due to the reinforced measures by Apple in macOS 10.15 Catalina to prevent non "notarized" apps from running.

The solution is probably to unquarantine the SageMath app. See more details at

2020-01-16 17:59:26 -0600 answered a question Sage 9.0 installation issues on macOS 10.15.2 Catalina

In macOS 10.15 Catalina, Apple pushes the security concerns and prevents apps from running unless they are "notarized", which means the developers need to register with Apple and get some certificate for the app.

Up to macOS 10.14 Mojave one could decide to run an app anyway, but in macOS 10.15 Catalina the option to do that is deactivated.

One can either restore that option by running the following command in a terminal (the initial $ on any line denotes the shell prompt, don't type that dollar sign):

$ sudo spctl --master-disable

Once that is done one recovers the option to bypass the quarantine by visiting "System Preferences" and going to the "Security" tab there, etc.

Or one can unquarantine a given application by running a command in the terminal instead of visiting the System Preferences. Say you already did the following:

  • download sage-9.0-OSX_10.15.2-x86_64.app.dmg

  • open the dmg and copy the app where you want it, for example in /Applications.

Then, to recursively unquarantine the app folder, do the following (change THING as needed if it's a different version of Sage or you changed its name or you put it in another location):

$ THING="/Applications/SageMath-9.0.app"
$ xattr -rd com.apple.quarantine $THING

I also recommend to create a symlink to the Sage executable somewhere in your path:

$ THING="/Applications/SageMath-9.0.app"
$ cd $THING/Contents/Resources/sage
$ sudo ln -s `pwd`/sage /usr/local/bin

Then you can start Sage in any terminal session, whatever directory you are working in.

To start the Sage REPL:

$ sage

To start the Jupyter notebook server:

$ sage -n jupyter

Note: in addition to lifting the "quarantine" on the app, some people have suggested one may need to give the app "full disk access" (by going to "System Preferences > Security > Privacy), see:

2020-01-07 06:51:44 -0600 received badge  Nice Answer (source)
2020-01-05 14:52:36 -0600 received badge  Nice Answer (source)
2020-01-04 17:39:09 -0600 commented answer Lengths of preperiod and period of a rational number

@geroyx. I edited the lines with print.

2020-01-04 17:38:43 -0600 edited answer Lengths of preperiod and period of a rational number

Length of Preperiod and Period

a = 1
b = 2*7

u = denominator(a/b).valuation(2)
v = denominator(a/b).valuation(5)

def PreperiodLength(a,b): return max(u,v)

m = denominator(a/b) / (2^u*5^v)
y = 1/m

def PeriodLength(a,b):
    if m == 1:
        return 0
    else:
        return y.period()

#print('m = {}'.format(m))
#print('{} / {} = {}'.format(a, b, float(a/b)))
print('{} / {} = {}'.format(a, b, numerical_approx(a/b, digits=PreperiodLength(a,b) + PeriodLength(a,b))))
print('PreperiodLength: {}'.format(PreperiodLength(a,b)))
print('PeriodLength: {}'.format(PeriodLength(a,b)))

Out:

1 / 14 = 0.07142857
PreperiodLength: 1
PeriodLength: 6
2020-01-04 17:34:29 -0600 commented question symbolic differentiation of unknown function

Note: I updated the answer to Ask Sage question 8822.

2020-01-04 17:33:17 -0600 edited question symbolic differentiation of unknown function

I want to do some formal calculus with unknown functions for the purpose of solving differential equations.

Say F(t) = v(t)*t^2, where v is an unknown differentiable function.

Then I would like to declare v as such and be able to get

F.diff(t) = 2*t*v+t^2*v.diff(t)

It is similar to Ask Sage question 8822 but the solution does not seem to work anymore, as function() takes only one argument and not 2 as in the description.

Anyone know what the syntax is in 8.9? Or in 9.0, when that becomes available?

2020-01-04 17:30:27 -0600 answered a question symbolic differentiation of unknown function

Here is how to do it.

Works with the following version of Sage.

sage: version()
'SageMath version 9.0, Release Date: 2020-01-01'

Declare variable t and function v, and define function F in terms of those:

sage: t = SR.var('t')
sage: v = function('v')
sage: F(t) = v(t)*t^2

Derivative of the function:

sage: F.diff(t)
t |--> t^2*diff(v(t), t) + 2*t*v(t)

Derivative of the expression:

sage: F(t).diff(t)
t^2*diff(v(t), t) + 2*t*v(t)
2020-01-04 17:25:44 -0600 edited answer Is there any way to define an as-yet-unknown function?

Answer from 2012. See further down for 2020 update.

You can define a function using the keyword function. The script below gives the answer you expect.

T = var('T')
a = function('a', T)
R = var('R')
v = var('v')
b = var('b')
p = R*T/v - a(T)/v/(v+b)
diff(p, T)

[2020 update] Nowadays, function no longer takes the variable as an argument.

Use a = function('a') instead of a = function('a', T).

Example:

sage: version()
'SageMath version 9.0, Release Date: 2020-01-01'
sage: b, v, R, T = SR.var('b v R T')
sage: a = function('a')
sage: p = R*T/v - a(T)/v/(v+b)
sage: diff(p, T)
R/v - diff(a(T), T)/((b + v)*v)
2019-12-24 15:53:27 -0600 commented answer How to install cantera on SAGE (OS X)?

The Python 3 problems should be solved by now. The Sage 8.x series has been increasingly Python3-compatible, and Sage 9.0 will be primarily Python3-based (unless one decides to build for Python 2, which will stay possible for a while).

2019-12-24 15:51:49 -0600 commented answer How to install cantera on SAGE (OS X)?

I turned your comment into an answer. Please accept the answer by clicking the "accept" button (with a check mark). This will mark the question as solved.

2019-12-24 08:53:42 -0600 received badge  Nice Answer (source)
2019-12-24 08:35:35 -0600 commented answer jmol cannot run while plot3d

@PHPirates -- was that with Firefox?

Viewing SageMath Three.js 3d plots in Firefox on Windows did not work in SageMath <= 8.9. But it worked in other browsers.

This problem was reported as

and fixed in

which was merged in Sage 9.0.beta9.

So, when Sage 9.0 comes out, viewing Three.js 3d plots in Firefox on Windows will work.

2019-12-23 23:07:12 -0600 received badge  Good Answer (source)
2019-12-23 22:27:25 -0600 commented answer Keeping zeros after matrix multiplication

I edited my answer to address your follow-up question.

2019-12-12 04:32:27 -0600 received badge  Nice Answer (source)
2019-12-03 23:59:20 -0600 answered a question Keeping zeros after matrix multiplication

(Edited to address the follow-up question posted as a comment to an earlier version of this answer).

Obtain a complete expression using string formatting:

sage: x, y, z = SR.var('x y z')
sage: u = vector([0, 1, 1])
sage: uu = vector([1, 2, -1])
sage: v = vector([x, y, z])
sage: print(' + '.join('({})*({})'.format(a, b) for a, b in zip(u, v)))
(0)*(x) + (1)*(y) + (1)*(z)
sage: print(' + '.join('({})*({})'.format(a, b) for a, b in zip(uu, v)))
(1)*(x) + (2)*(y) + (-1)*(z)

The parentheses are maybe not so nice but they take care of keeping things clean whether coefficients are positive, negative or zero.

If one wants to push the "pretty-printing" effort a little, one should probably write a function. For example:

def axbycz(u):
    r"""
    Return a custom string for the scalar product of
    this vector with the vector (x, y, z), keeping zeros.

    EXAMPLES::

        sage: axbycz(vector([0, 1, 1]))
        '0x + y + z'
        sage: axbycz(vector([1, 2, -1]))
        'x + 2y - z'
        sage: print(axbycz(vector([0, 1, 1])))
        0x + y + z
        sage: print(axbycz(vector([1, 2, -1])))
        x + 2y - z
    """
    aa = lambda c: '' if c == 1 else '-' if c == -1 else c
    bc = lambda c: '+ {}'.format(aa(c)) if c >=0 else '- {}'.format(aa(-c))
    a, b, c = u
    return '{}x {}y {}z'.format(aa(a), bc(b), bc(c))

where the output will be as in the examples included in the function's documentation.

One could instead decide to have the function print a result rather than return a string.

def print_axbycz(u):
    r"""
    Print a custom string for the scalar product of
    this vector with the vector (x, y, z), keeping zeros.

    EXAMPLES::

        sage: print_axbycz(vector([0, 1, 1]))
        0x + y + z
        sage: print_axbycz(vector([1, 2, -1]))
        x + 2y - z
    """
    aa = lambda c: '' if c == 1 else '-' if c == -1 else c
    bc = lambda c: '+ {}'.format(aa(c)) if c >=0 else '- {}'.format(aa(-c))
    a, b, c = u
    print('{}x {}y {}z'.format(aa(a), bc(b), bc(c)))
2019-11-24 06:13:53 -0600 received badge  Popular Question (source)
2019-11-10 23:27:07 -0600 answered a question Output of a sequence in Display and as a txt-File

One way would be as follows:

with open('collatz_{}_{}.txt'.format(X, N), 'w') as file:
        file.writelines(str(k) + '\n' for k in f(X, N))

Also, you could use x = x // 2 in your code to always work with integers rather than rational numbers.

2019-10-30 14:21:46 -0600 received badge  Nice Answer (source)
2019-10-29 21:00:00 -0600 answered a question Error when importing Numpy

When uninstalling with pip, make sure to use SageMath's pip:

sage --pip uninstall numpy
2019-10-29 06:11:54 -0600 received badge  Great Answer (source)
2019-10-08 07:37:14 -0600 answered a question Creating a vector of indexed variables

The Python identifier can't have brackets so this fails:

sage: var('x[0]')
Traceback (most recent call last)
...
ValueError: The name "x[0]" is not a valid Python identifier.

but the LaTeX name can, so you can do:

sage: X = [SR.var('x_{}'.format(i), latex_name='x[{}]'.format(i)) for i in (0 .. 7)]

and then you would have in the Sage REPL:

sage: X
[x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7]
sage: show(X)
\newcommand{\Bold}[1]{\mathbf{#1}}\left[{x[0]}, {x[1]}, {x[2]}, {x[3]}, {x[4]}, {x[5]}, {x[6]}, {x[7]}\right]

but in a Jupyter notebook, show(X) would give a LaTeX-rendered $$[x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7]]$$

2019-10-08 05:45:12 -0600 answered a question Sagemath 8.8 crash on startup on windows 10

The SageMath installer works by installing a "Linux-like" layer for Windows called Cygwin. This operates at a rather low level, and various anti virus scanners tend to consider this low-level activity as "virus-like", and as a consequence they tend to get in the way of the SageMath installer, or of the Cygwin it installs. Depending on the antivirus software and how one configures it, it might just raise an alert, or block the software it thinks is virus-like, or try to destroy it.

Recommendations:

  • download the SageMath installer for Windows
  • tell all antivirus software to ignore the SageMath installer for Windows
  • run the installer; this installs SageMath
  • tell the antivirus software to ignore SageMath
  • run SageMath

If one runs the installer, or SageMath, before having told antivirus software to ignore it, and if antivirus software considers some of the low-level activity by the installer or by SageMath as virus-like, then such antivirus software might decide not only to block the installer or SageMath, but to remove it, or parts of it, making it non-working later on, even if one later tells the antivirus software to ignore it.

This applies not only to "trend micro" but also to "windows defender", "kaspersky", etc.

2019-10-08 05:41:16 -0600 commented question Sagemath 8.8 crash on startup on windows 10

Trend Micro is a cyber security company, and Trend Micro office scan is likely an anti-virus scanner by that company.

2019-10-07 10:52:21 -0600 answered a question Factoring vs Prime identification

Yes, checking that a number is not prime can be very easy, without factoring it: you just check that it does not satisfy some condition that all primes satisfy.

For example, if $n$ is a prime, then for any integer $a$, we have $a^n$ is congruent to $a$ modulo $n$.

Another way to say that is that if $n$ is an integer, and if there is any integer $a$ such that $a^n$ is not congruent to $a$ modulo $n$, then $n$ is not prime. Finding such an $a$ can be very cheap but it would not give a way to factor $n$.

See the notion of probable prime and pseudo-prime.

2019-10-06 10:32:16 -0600 commented question Hypergeometric Bug?

To display blocks of code or error messages, skip a line above and below, and do one of the following (all give the same result):

  • indent all code lines with 4 spaces
  • select all code lines and click the "code" button (the icon with '101 010')
  • select all code lines and hit ctrl-K

For instance, typing

If we define `f` by

    def f(x, y, z):
        return x * y * z

then `f(2, 3, 5)` returns `30` but `f(2*3*5)` gives:

    TypeError: f() takes exactly 3 arguments (1 given)

produces:

If we define f by

def f(x, y, z):
    return x * y * z

then f(2, 3, 5) returns 30 but f(2*3*5) gives:

TypeError: f() takes exactly 3 arguments (1 given)

Please edit your question to do that.

2019-10-03 08:13:51 -0600 commented question expand large expressions made of complex functions and variables

To display blocks of code or error messages, skip a line above and below, and do one of the following (all give the same result):

  • indent all code lines with 4 spaces
  • select all code lines and click the "code" button (the icon with '101 010')
  • select all code lines and hit ctrl-K

For instance, typing

If we define `f` by

    def f(x, y, z):
        return x * y * z

then `f(2, 3, 5)` returns `30` but `f(2*3*5)` gives:

    TypeError: f() takes exactly 3 arguments (1 given)

produces:

If we define f by

def f(x, y, z):
    return x * y * z

then f(2, 3, 5) returns 30 but f(2*3*5) gives:

TypeError: f() takes exactly 3 arguments (1 given)

Please edit your question to do that.

2019-10-01 10:08:31 -0600 marked best answer Install optional packages (eg extra Jupyter kernels) on top of conda-installed SageMath

Suppose one has installed SageMath using the conda package.

How does one then install optional packages? It seems sage -i is not active there.

For example, what would be the replacement for

$ sage -i gap_jupyter         # install Jupyter kernel for GAP 4.8
$ sage -i pari_jupyter        # install Jupyter kernel for PARI/GP
$ sage -i r_jupyter           # install Jupyter kernel for R
$ sage -i singular_jupyter    # install Jupyter kernel for Singular

if one is using a conda-installed Jupyter and SageMath?

2019-09-24 15:07:21 -0600 commented question Illustrate column picture of matrix multiplication between a 3 x 4 matrix and a 4 x 2 matrix?

Could you post code that can be copy-pasted into a fresh Sage session to illustrate your problem?

Please edit the question to do that.

2019-09-24 15:07:18 -0600 commented question Illustrate column picture of matrix multiplication between a 3 x 4 matrix and a 4 x 2 matrix?

Welcome to Ask Sage. Thank you for your question.

2019-09-04 06:23:21 -0600 received badge  Nice Answer (source)
2019-09-01 12:54:35 -0600 answered a question donation button on Sagemath.org ?

Thanks for your appreciation of SageMath! I find a lot of happiness in it too!

There used to be a donation button for donating to "The Sage Foundation", a foundation supporting SageMath which was operated by the University of Washington; however this foundation has been discontinued.

SageMath applying to become a sponsored project of NumFOCUS (or of its European equivalent, which is in the process of being created) is regularly discussed; this has not happened yet but I am sure it will become a reality in the not so distant future.

In the meantime, you could make a donation to one or several of the NumFOCUS-sponsored projects that underly SageMath, for example NumPy, Matplotlib, Jupyter, IPython, SymPy, Conda-Forge, MathJax... Or to other NumFOCUS-affiliated projects, for example Cython... Or to the Python Software Foundation.

The "14 USD/month" you refer to is probably a CoCalc plan; note that you can subscribe to it once in a while for just a month or two, if that is the kind of amount you want to spend.

Another way to help is to get Google Cloud starting credits and to set up some GitLab runners to help with the continuous development / continuous integration of SageMath. See the GitLab-CI page on the SageMath wiki.

2019-08-31 11:32:30 -0600 edited answer How do I install opencv in SageMath 8.8 on Windows 10

OpenCV-Python provides Python bindings for OpenCV (free software for computer vision).

To install it you need OpenCV itself, as well as Python, to be installed.

Maybe try the following, in a Sage shell (which, if you installed Sage using the Sage-Windows installer, runs in Cygwin):

apt-cyg install libopencv-devel

Then install opencv-python with the command:

pip install opencv-python
2019-08-28 05:49:43 -0600 commented question Sage not detecting integers in Boolean Polynomial Ring?

Note: instead of

for i in range(len(equations)):
    for j in equations[i]:
        if j == 1:
            print 'true'

you could use

for eq in equations:
    for j in eq:
        if j == P.one():
            print('true')