Ask Your Question

Dan-K's profile - activity

2023-07-06 03:48:01 +0200 answered a question Getting LaTeX code from Sage when running %display latex

If you connect VSCode to your Sage+Jupyter instance, you can toggle the output between text/plain, text/html, and text/l

2023-07-06 03:46:15 +0200 edited answer Render Latex in IPython notebook and display on output line

I just wanted to add an alternative to @eric_g's answer which also worked for me. If you're using Sage with VSCode, the

2023-07-06 03:39:34 +0200 answered a question Render Latex in IPython notebook and display on output line

I just wanted to add an alternative to @eric_g's answer which also worked for me. In the jupyter interface (or jupyter-

2023-07-01 19:03:36 +0200 received badge  Notable Question (source)
2023-06-29 01:02:08 +0200 marked best answer Dashed line in a one variable parametric_plot3d

Say you have a 3d line parametric plot (or parametric_plot3d with one variable). How can you stile the resulting plotted line in a dashed style?

Checked out the documentation and I noticed parametric_plot3d has an option for border_style. However it looks like this has no affect when the area is zero.

There appears to be linestyle available to 2d plots but this has no effect in 3d plots whether specified directly or in border_style.

I also tried giving the colour a colourmap via colormaps.autumn but I get an error:

TypeError: unable to simplify to float approximation

Example

Below is the specific example I'm trying to dash. It is the second parametric_plot3d with the comments:

var(['x', 'y', 'z', 'u', 'v', 'r', 'theta'])

angle = 30 * pi / 180

radius = tan(angle)*z

show(
    parametric_plot3d(
        (
            r*cos(theta),
            r*sin(theta),
            r/tan(30 * pi / 180)
        ),
        (r, 0, radius(z = 1)),
        (theta, 0, 2*pi),
        opacity = 0.5
    )
    + point3d((radius(z = 0.5), 0, 0.5), color = 'red')
    + parametric_plot3d(
        (
            radius(z = 0.5)*cos(theta),
            radius(z = 0.5)*sin(theta),
            0.5
        ),
        (theta, 0, 2*pi),
        # color = 'red', # Produces a solid red line
        # color = (theta, colormaps.autumn) # Throws an error
        # linestyle = '--', # Does not work
        # boundary_style = {  # Does not work
        #   'thickness': 5,
        #   'linestyle': '--',
        #   'color': 'red'
        # }
    )
)
2023-06-29 01:01:51 +0200 received badge  Commentator
2023-06-29 01:01:51 +0200 commented answer Dashed line in a one variable parametric_plot3d

Thanks for your answer! Yes I am aware of the difference between parametric_plot and parametric_plot3d, or at least I th

2023-06-29 00:57:16 +0200 commented answer Dashed line in a one variable parametric_plot3d

Thanks for your answer! The first one works pretty well. Not the best looking but the best I've seen so far, thank you!

2023-06-29 00:56:49 +0200 commented answer Dashed line in a one variable parametric_plot3d

Thanks for your answer! The first one works pretty well. Not the best looking but the best I've seen so far, thank you!

2023-06-27 00:34:52 +0200 received badge  Popular Question (source)
2023-06-27 00:23:16 +0200 asked a question Dashed line in a one variable parametric_plot3d

Dashed line in a one variable parametric_plot3d Say you have a 3d line parametric plot (or parametric_plot3d with one va

2022-04-18 16:26:58 +0200 received badge  Popular Question (source)
2022-04-12 00:54:29 +0200 received badge  Necromancer (source)
2022-04-12 00:54:29 +0200 received badge  Self-Learner (source)
2022-04-11 07:02:35 +0200 edited question How to flip normals of Graphics3d faces?

Built-in way to flip normals of Graphics3D mesh? Is there an easy way to flip the normals for all the faces of a sage.pl

2022-04-11 06:34:38 +0200 commented question issue with sagemath on windows 11

What installation instructions are you following?

2022-04-11 00:57:17 +0200 answered a question enforce limits to x-axis in parametric_plot

From the documentation it looks like sage.plot.animate.animate has parameters xmin, xmax, ymin, ymax: b = animate(sprin

2022-04-10 23:53:00 +0200 asked a question How to flip normals of Graphics3d faces?

Built-in way to flip normals of Graphics3D mesh? Is there an easy way to flip the normals for all the faces of a sage.pl

2022-04-10 22:49:54 +0200 edited answer Obtaining a RealNumber in Python

RealNumber can be imported and used from sage.all: from sage.all import RealNumber # ... test = RealNumber(1.2)

2022-04-10 22:45:17 +0200 answered a question Obtaining a RealNumber in Python

RealNumber can be imported from sage.all: from sage.all import RealNumber

2021-04-01 14:00:33 +0200 edited answer Sage is not updating modules imported by an attached file

Not sure if this works for the sage shell, but adding the following two lines successfully reloads modules on every exec

2021-04-01 13:59:34 +0200 answered a question Sage is not updating modules imported by an attached file

Not sure if this works for the sage shell, but adding the following two lines successfully reloads modules on every exec

2021-03-30 16:18:12 +0200 edited question Obtaining a RealNumber in Python

Obtaining a RealNumber in Python I have a python module imported by a SageMath notbook and in this module, I import and

2021-03-30 16:17:44 +0200 edited question Obtaining a RealNumber in Python

Obtaining a RealNumber in Python I have a python module imported by a SageMath notbook and in this module, I import and

2021-03-30 15:53:45 +0200 commented answer Obtaining a RealNumber in Python

Thank you so much!

2021-03-30 15:53:10 +0200 marked best answer Obtaining a RealNumber in Python

I have a python module imported by a SageMath notbook and in this module, I import and use Sage functionality via from sage.all import *.

Most functions work fine, however when I try to obtain a RealNumber such as in the following code:

from sage.rings.real_mpfr import RealNumber
# ...
test = RealNumber(1.2)

I get this error:

TypeError: Cannot convert float to sage.rings.real_mpfr.RealField_class

I've tried using strings as well and that didn't work either.

Note that to use RealNumber I need to import it explicitly (it's not included in sage.all). <- This was an incorrect assumption on my part. After @slelievre's answer, I looked if create_RealNumber was imported by sage.all and it indeed is. Ultimately, to get RealNumber to work, only the sage.all import is needed.

Question:

How can I use RealNumber in my Python module?

2021-03-30 07:49:35 +0200 asked a question Obtaining a RealNumber in Python

Obtaining a RealNumber in Python I have a python module imported by a SageMath notbook and in this module, I import and

2021-02-15 03:29:03 +0200 commented question blender interface

I often export from Sage with STL format and import that with Blender. It works great. You can use sage.plot.plot3d.base.Graphics3dGroup.save(...) to do this.

2020-05-23 17:23:26 +0200 marked best answer Can't post answers on this site

Help! I can't post answers to questions on this site (http://ask.sagemath.org).

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.

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

You sweet genius

2020-02-15 20:45:59 +0200 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 15:29:45 +0200 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 13:37:31 +0200 received badge  Necromancer (source)
2020-02-02 13:37:31 +0200 received badge  Teacher (source)
2020-01-31 21:36:23 +0200 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: http://www.cecm.sfu.ca/sage/win/SageM...

2020-01-31 21:22:07 +0200 edited answer 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

or:

/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

or:

/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\asyncio.py file (example: C:\Users\<User>\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\tornado\platform\asyncio.py) and add the following right after import asyncio:

import sys
if sys.platform == 'win32':
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
  • 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": [
        "/opt/sagemath-9.0/local/bin/sage",
        "--python",
        "-m",
        "sage.repl.ipython_kernel",
        "-f",
        "{connection_file}"
    ],
    "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/SageMath-9.0.app/Contents/Resources/sage/local/bin/python3.

  • 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": [
        "/Applications/SageMath-9.0.app/Contents/Resources/sage/local/bin/python3",
        "-m",
        "sage.repl.ipython_kernel",
        "-f",
        "{connection_file}"
    ],
    "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-31 20:06:00 +0200 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 08:10:17 +0200 commented question SageMath, VSCode and python.pythonPath

Check out the answer I posted on https://ask.sagemath.org/question/432...