Ask Your Question

Dan-K's profile - activity

2020-05-16 12:55:01 -0500 received badge  Nice Answer (source)
2020-02-15 14:13:09 -0500 commented answer Symbolic expression of `sng()` which isn’t zero at `sgn(0)`

You sweet genius

2020-02-15 13:45:59 -0500 asked a question Symbolic expression of `sng()` which isn’t zero at `sgn(0)`

sng(0) = 0 but I need a symbolic function or expression that evaluates to +1 or -1 as per the following definition:

$$\mathrm{side}\left(u\right) = \begin{cases} +1, & \text{if $u \geq 0$} \\ -1, & \text{if $u \lt 0$} \end{cases}$$

I’ve tried the following but each have their own problems:

side = sgn(u) # Evaluates to 0 at u = 0
side = u/abs(u) # “ValueError: power::eval(): division by zero” at u = 0
side = 1 - (u < 0)*2 # “TypeError: unable to simplify to float approximation”
# These next two use a Python expression so ‘u’ gets evaluated too early.
side = -1 if u < 0 else 1
side = lambda u: -1 if u < 0 else 1

Is there a way I can define this function symbolically?

2020-02-03 08:29:45 -0500 commented answer Sagemath and Vscode

The Python log output in VSCode shows the process VSCode uses to find and validate each Python interpreter, including any one you explicitly set via "python.pythonPath" option. You'll find that it runs the command I mentioned and for Sage's Python the output is a POSIX path instead of a Windows one (since the executable runs under a cygwin environment).

2020-02-02 06:37:31 -0500 received badge  Necromancer (source)
2020-02-02 06:37:31 -0500 received badge  Teacher (source)
2020-01-31 14:36:23 -0500 answered a question Download Ver 8.9

From hereyou can see a list of versions of Sage. Here's a direct link for 8.9:

2020-01-31 13:06:00 -0500 commented answer Sagemath and Vscode

That's a good observation @Iguananaut! I didn't mean to imply VSCode has its own Python, but rather, it has a process for detecting installed Python environments. Unfortunately on Windows, it's not able to detect Sage's Python executable because VSCode uses "import sys; print(sys.executable)" to get the actual path, and since Sage's python is in a cygwin environment, it finds it under /usr/bin/python3 or something like that.

For Mac OSX (and probably Linux) you can totally do what you suggest, as Sage's Python can be detected as per step Common.2.

However VSCode needs a copy of Jupyter executables to function properly, and since it can't use Sage's ones on Windows, they need to be installed, even if we're connecting to an externally running Jupyter server.

2020-01-31 01:10:17 -0500 commented question SageMath, VSCode and python.pythonPath

Check out the answer I posted on

2020-01-30 16:24:15 -0500 commented answer how to find a local maximum?

The thing that's wrong with what you propose @Emmanuel Charpentier is that it doesn't find the local maximum. For example take y = 2*x + 3; find_local_maximum(y, 1, 4) will return (10.999999837732908, 3.9999999188664543) while y.diff(x).solve(x) returns []

2020-01-30 14:46:52 -0500 received badge  Editor (source)
2020-01-30 14:46:03 -0500 answered a question Sagemath and Vscode

VSCode (1.41.1) doesn't officially support kernels other than Python (8521, 5078) but you can still get it to work by setting the kernel's "language" to "python".

Here are steps to use Sage in VSCode assuming you have Sage and VSCode already installed.

Please follow both the common steps and the platform steps specific to your platform.

Common steps (Windows & Mac OSX)

  • Common.1. Add the 'Python' VSCode extension and make sure it's enabled. This can be done by:

    • a. Pressing the extensions icon on the left icon bar within VSCode.
    • b. Typing in 'python' into the search bar.
    • c. Selecting the 'Python' extension by Microsoft.
    • d. Install or enable the extension.
  • Common.2. Pick any python environmnet recognised by VSCode.

It can be any environment you've configured for VSCode, or one automatically detected by VSCode. Once you've picked the environment, take note of the paths to the python executable for that environment in order to execute the next 2 step's commands.

Note: There are some issues with VSCode which prevent you from choosing Sage's Python environment in Windows.

  • Common.3. Install Jupyter Notebook for your selected environment.

Do this by running the following command:

/path/to/python -m pip install notebook


/path/to/python -m pip install notebook --user
  • Common.4. Install the default Python kernel.

This is required or else VSCode won’t work with Jupyter properly.

/path/to/python -m ipykernel install


/path/to/python -m ipykernel install --user

Windows Steps (scroll down for Mac OSX)

The Windows solution involves launching the installed SageMath Notebook shortcut and connecting VSCode to this running instance via the python.dataScience.jupyterServerURI VSCode setting.

  • Windows.1. Upgrade notebook Python module.

This step was taken before the next one because it may have a fix for the next step, in which case the next step can be skipped.

path\to\python -m pip install notebook --upgrade

In the target python environment identified in step Common.2, modify this Python environment's Lib\site-packages\tornado\platform\ file (example: C:\Users\<User>\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\tornado\platform\ and add the following right after import asyncio:

import sys
if sys.platform == 'win32':
  • Windows.3. Navigate to SageMath's Jupyter kernel directory which is typically C:\Users\<User>\AppData\Local\SageMath-9.0\runtime\opt\sagemath-9.0\local\share\jupyter\kernels.

  • Windows.4. Duplicate the sagemath folder and name it sagemath-vscode.

  • Windows.5. Edit the new sagemath-vscode folder's kernel.json by changing the "language" value to "python" and change "display_name" to "SageMath 9.0 for VSCode".

C:\Users\<User>\AppData\Local\SageMath-9.0\runtime\opt\sagemath-9.0\local\share\jupyter\kernels\sagemath-vscode\kernel.json should look like this:

    "argv": [
    "display_name": "SageMath 9.0 for VSCode",
    "language": "python"
  • Windows.6. Launch the SageMath 9.0 Notebook shortcut which was created by the SageMath installer.

  • Windows.7. Take note of the URL including the token value from the console output from the previous step.

  • Windows.8. Modify your VSCode's User or Workspace setting for python.dataScience.jupyterServerURI to the URL identified in the previous step.

Note: For Windows, you need to update the python.dataScience.jupyterServerURI everytime you re-run the SageMath 9.0 Notebook shortcut.

Proceed to the last section!

Mac OSX Steps

  • MacOSX.1. Take note of the path to your SageMath's python executable. This is typically found in the following folder /Applications/

  • MacOSX.2. Locate your Jupyter kernel folder. This can be seen from the output of step Common.4 and is typically ~/Library/Jupyter/kernels.

  • MacOSX.3. Create a folder named sagemath-vscode in the kernel folder identified in the previous step.

  • MacOSX.4. Create a file named kernel.json in the sagemath-vscode folder which was just created and populate it with the following content.

Make sure the python path you enter here is the same as the one identified in step MacOSX.1:

    "argv": [
    "display_name": "SageMath 9.0 for VSCode",
    "language": "python"

You can now use SageMath with VSCode!

  1. Create or open a notebook.
  2. At the bottom right, select the python environment identified in step Common.2.
  3. Run a cell.
  4. Change the kernel at the top right to SageMath 0.9 for VSCode.
2020-01-29 09:04:14 -0500 answered a question how to find a local maximum?

Not sure when the change occurred, but in SageMath 9.0, find_maximum_on_interval has been replaced with find_local_maximum.

Bear in mind this is a numerical method.

2020-01-26 01:46:43 -0500 received badge  Scholar (source)
2020-01-26 01:46:39 -0500 received badge  Supporter (source)
2020-01-22 06:58:51 -0500 received badge  Good Question (source)
2020-01-21 16:24:04 -0500 received badge  Nice Question (source)
2020-01-21 15:25:55 -0500 received badge  Student (source)
2020-01-21 15:03:58 -0500 asked a question Can't post answers on this site

Help! I can't post answers to questions on this site (

I've posted an answer to this question (/question/43240/sagemath-and-vscode/ sorry I can't even post links) a few days ago but it's not showing up.

This was the first thing I tried to post. Are there any other steps I need to make before posting an answer??

The first time I pressed Post Your Answer nothing showed up so I tried posting it a few more times. I hope this didn't affect my ability to post anything.