Symbolic math¶

go to console by clicking the + button and install it with

https://docs.sympy.org/latest/tutorials/intro-tutorial/intro.html

In [2]:
%load_ext rpy2.ipython
Error importing in API mode: ImportError('/home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages/_rinterface_cffi_api.abi3.so: undefined symbol: R_ClosureEnv')
Trying to import in ABI mode.
In [3]:
! pip install sympy
Requirement already satisfied: sympy in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (1.14.0)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from sympy) (1.3.0)
In [4]:
import sympy
sympy.sqrt(3)
Out[4]:
$\displaystyle \sqrt{3}$

numeric python¶

go to console by clicking the + button and install it with

https://numpy.org/doc/stable/user/quickstart.html

In [5]:
! pip install numpy
Requirement already satisfied: numpy in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (2.4.3)
In [6]:
import numpy as np

# Create arrays
a = np.array([1, 2, 3, 4, 5])
b = np.linspace(0, 1, 5)          # 5 evenly spaced values from 0 to 1

# Basic operations (element-wise)
print("a       :", a)
print("b       :", b)
print("a + b   :", a + b)
print("a * 2   :", a * 2)
print("a²      :", a ** 2)

# 2-D array (matrix)
M = np.array([[1, 2], [3, 4]])
print("\nMatrix M:\n", M)
print("Transpose:\n", M.T)
print("Matrix product M @ M:\n", M @ M)

# Aggregate functions
print("\nmean(a):", np.mean(a))
print("std(a) :", np.std(a))
print("sum(a) :", np.sum(a))
a       : [1 2 3 4 5]
b       : [0.   0.25 0.5  0.75 1.  ]
a + b   : [1.   2.25 3.5  4.75 6.  ]
a * 2   : [ 2  4  6  8 10]
a²      : [ 1  4  9 16 25]

Matrix M:
 [[1 2]
 [3 4]]
Transpose:
 [[1 3]
 [2 4]]
Matrix product M @ M:
 [[ 7 10]
 [15 22]]

mean(a): 3.0
std(a) : 1.4142135623730951
sum(a) : 15
In [7]:
%%R
# R equivalent of the numpy code above

# Create arrays (vectors)
a <- c(1, 2, 3, 4, 5)
b <- seq(0, 1, length.out = 5)   # same as np.linspace(0, 1, 5)

# Basic operations (element-wise) — same behaviour as numpy
cat("a       :", a, "\n")
cat("b       :", b, "\n")
cat("a + b   :", a + b, "\n")
cat("a * 2   :", a * 2, "\n")
cat("a²      :", a ^ 2, "\n")    # ** in Python  →  ^ in R

# 2-D array (matrix)
# DIFFERENCE: R fills matrices column-major by default → byrow=TRUE needed
M <- matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
cat("\nMatrix M:\n"); print(M)
cat("Transpose:\n"); print(t(M))          # t()  instead of  .T
cat("Matrix product M %*% M:\n"); print(M %*% M)  # %*%  instead of  @

# Aggregate functions
cat("\nmean(a):", mean(a), "\n")
# DIFFERENCE: R's sd() divides by n-1 (sample std dev),
#             numpy's std() divides by n (population std dev) by default.
#             Population version in R: sd(a) * sqrt((length(a)-1)/length(a))
cat("sd(a)  :", sd(a), "\n")
cat("sum(a) :", sum(a), "\n")
a       : 1 2 3 4 5 
b       : 0 0.25 0.5 0.75 1 
a + b   : 1 2.25 3.5 4.75 6 
a * 2   : 2 4 6 8 10 
a²      : 1 4 9 16 25 

Matrix M:
     [,1] [,2]
[1,]    1    2
[2,]    3    4
Transpose:
     [,1] [,2]
[1,]    1    3
[2,]    2    4
Matrix product M %*% M:
     [,1] [,2]
[1,]    7   10
[2,]   15   22

mean(a): 3 
sd(a)  : 1.581139 
sum(a) : 15 

Mathplotlib¶

go to console by clicking the + button and install it with

https://matplotlib.org/stable/tutorials/pyplot.html#sphx-glr-tutorials-pyplot-py

In [8]:
! pip install matplotlib
#! conda install matplotlib -y
Requirement already satisfied: matplotlib in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (3.10.9)
Requirement already satisfied: contourpy>=1.0.1 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (1.3.3)
Requirement already satisfied: cycler>=0.10 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (4.62.1)
Requirement already satisfied: kiwisolver>=1.3.1 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (1.5.0)
Requirement already satisfied: numpy>=1.23 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (2.4.3)
Requirement already satisfied: packaging>=20.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (26.1)
Requirement already satisfied: pillow>=8 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (12.2.0)
Requirement already satisfied: pyparsing>=3 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (3.3.2)
Requirement already satisfied: python-dateutil>=2.7 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib) (2.9.0.post0)
Requirement already satisfied: six>=1.5 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0)
In [9]:
import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4], marker='o')
plt.ylabel('some numbers')
plt.show()
No description has been provided for this image
In [10]:
%%R
# R equivalent of the matplotlib code above

# plot() is base R — no library needed (unlike matplotlib which must be imported)
# DIFFERENCE: R's plot() draws points by default → type="b" for line + points
#             matplotlib equivalent: plt.plot([1,2,3,4], marker='o')
plot(c(1, 2, 3, 4),        # c(1,2,3,4)  is the same as  [1, 2, 3, 4]
     type = "b",            # "b" = both line and points
     ylab = "some numbers"  # ylab=  instead of  plt.ylabel()
)
# DIFFERENCE: plot() displays immediately — no plt.show() call required
No description has been provided for this image

Scipi¶

https://docs.scipy.org/doc/scipy/tutorial/index.html#user-guide

Interactive matplotlib¶

https://saturncloud.io/blog/python-matplotlib-make-3d-plot-interactive-in-jupyter-notebook/#:~:text=Remember%20that%20the%20key%20to,the%20data%20in%20three%20dimensions.

In [11]:
! pip install scipy
Requirement already satisfied: scipy in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (1.17.1)
Requirement already satisfied: numpy<2.7,>=1.26.4 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from scipy) (2.4.3)
In [12]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
! pip install ipympl
#! conda install ipympl -y
#%matplotlib widget
Requirement already satisfied: ipympl in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (0.10.0)
Requirement already satisfied: ipython<10 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipympl) (9.12.0)
Requirement already satisfied: ipywidgets<9,>=7.6.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipympl) (8.1.8)
Requirement already satisfied: matplotlib<4,>=3.5.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipympl) (3.10.9)
Requirement already satisfied: numpy in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipympl) (2.4.3)
Requirement already satisfied: pillow in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipympl) (12.2.0)
Requirement already satisfied: traitlets<6 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipympl) (5.14.3)
Requirement already satisfied: decorator>=5.1.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython<10->ipympl) (5.2.1)
Requirement already satisfied: ipython-pygments-lexers>=1.0.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython<10->ipympl) (1.1.1)
Requirement already satisfied: jedi>=0.18.2 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython<10->ipympl) (0.19.2)
Requirement already satisfied: matplotlib-inline>=0.1.6 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython<10->ipympl) (0.2.1)
Requirement already satisfied: pexpect>4.6 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython<10->ipympl) (4.9.0)
Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython<10->ipympl) (3.0.52)
Requirement already satisfied: pygments>=2.14.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython<10->ipympl) (2.20.0)
Requirement already satisfied: stack_data>=0.6.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython<10->ipympl) (0.6.3)
Requirement already satisfied: comm>=0.1.3 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipywidgets<9,>=7.6.0->ipympl) (0.2.3)
Requirement already satisfied: widgetsnbextension~=4.0.14 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipywidgets<9,>=7.6.0->ipympl) (4.0.15)
Requirement already satisfied: jupyterlab_widgets~=3.0.15 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipywidgets<9,>=7.6.0->ipympl) (3.0.16)
Requirement already satisfied: contourpy>=1.0.1 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib<4,>=3.5.0->ipympl) (1.3.3)
Requirement already satisfied: cycler>=0.10 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib<4,>=3.5.0->ipympl) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib<4,>=3.5.0->ipympl) (4.62.1)
Requirement already satisfied: kiwisolver>=1.3.1 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib<4,>=3.5.0->ipympl) (1.5.0)
Requirement already satisfied: packaging>=20.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib<4,>=3.5.0->ipympl) (26.1)
Requirement already satisfied: pyparsing>=3 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib<4,>=3.5.0->ipympl) (3.3.2)
Requirement already satisfied: python-dateutil>=2.7 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from matplotlib<4,>=3.5.0->ipympl) (2.9.0.post0)
Requirement already satisfied: wcwidth in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython<10->ipympl) (0.6.0)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from jedi>=0.18.2->ipython<10->ipympl) (0.8.6)
Requirement already satisfied: ptyprocess>=0.5 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from pexpect>4.6->ipython<10->ipympl) (0.7.0)
Requirement already satisfied: six>=1.5 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from python-dateutil>=2.7->matplotlib<4,>=3.5.0->ipympl) (1.17.0)
Requirement already satisfied: executing>=1.2.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython<10->ipympl) (2.2.1)
Requirement already satisfied: asttokens>=2.1.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython<10->ipympl) (3.0.1)
Requirement already satisfied: pure_eval in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython<10->ipympl) (0.2.3)
In [13]:
# Generating random data
np.random.seed(42)
x = np.random.randint(0, 100, size=100)
y = np.random.randint(0, 100, size=100)
z = np.random.randint(0, 100, size=100)

# Creating a 3D scatter plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='r', marker='o')

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()
No description has been provided for this image
In [14]:
%%R
install.packages("scatterplot3d", repos = "https://cloud.r-project.org")
* installing *source* package ‘scatterplot3d’ ...
** this is package ‘scatterplot3d’ version ‘0.3-45’
** package ‘scatterplot3d’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (scatterplot3d)
trying URL 'https://cloud.r-project.org/src/contrib/scatterplot3d_0.3-45.tar.gz'
Content type 'application/x-gzip' length 484624 bytes (473 KB)
==================================================
downloaded 473 KB


The downloaded source packages are in
	‘/tmp/RtmpK2v5cQ/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
In [15]:
%%R
# R equivalent of the 3D scatter plot above

# DIFFERENCE: requires the 'scatterplot3d' package (not base R)
#             Python uses mpl_toolkits.mplot3d (part of matplotlib)
# install.packages("scatterplot3d")  # run once if not installed
library(scatterplot3d)

# Generating random data
set.seed(42)                              # same as np.random.seed(42)
x <- sample(0:99, 100, replace = TRUE)   # same as np.random.randint(0, 100, size=100)
y <- sample(0:99, 100, replace = TRUE)
z <- sample(0:99, 100, replace = TRUE)

# Creating a 3D scatter plot
# DIFFERENCE: scatterplot3d() is a single call — no fig/ax objects needed
scatterplot3d(x, y, z,
              color = "red",   # c='r'  in matplotlib
              pch   = 16,      # filled circle — equivalent of marker='o'
              xlab  = "X Label",
              ylab  = "Y Label",
              zlab  = "Z Label"
)
# DIFFERENCE: displays immediately — no plt.show() required
No description has been provided for this image
In [16]:
# diese Zelle durch Eingabe von <shift>+<return> ausführen
## allgemein nützliche Einstellungen und Pakete
# Grafiken im Notebook anzeigen
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
In [17]:
# diese Zelle durch Eingabe von <shift>+<return> ausführen
# erstes Beispiel
# ---------------
# Felder mit Datenpunkten erzeugen
x = np.linspace(-20., 20., 200)
y = np.sin(x)
# Abbildung erzeugen
plt.plot(x,y)
# Abbildung darstellen
plt.show()
No description has been provided for this image

R¶

we can use %%R -i variable to get values from pythonn.

In [ ]:
%%R -i x -i y
# R equivalent of the sine plot above

# Felder mit Datenpunkten erzeugen
x2 <- seq(-20, 20, length.out = 200)  # same as np.linspace(-20., 20., 200)
y2 <- cos(x2)                          # sin() instead of np.sin()

# Draw first series, then overlay the second on the same axes
plot(x2, y2,
     type = "l",
     col = "blue",
     lwd = 2,
     xlab = "x",
     ylab = "y",
     main = "Combined plot: cos(x2) and y(x)"
 )
 # Overlay the second series (y vs x) on the same plot
 # by using lines() to add to the existing plot without erasing it
 # instead of plot() which would create a new plot and erase the first one.
lines(x, y,
      type = "b",
      col = "red",
      lwd = 2,
      pch = 16
 )
legend("topright",
       legend = c("cos(x2)", "y(x)"),
       col = c("blue", "red"),
       lty = 1,
       pch = c(NA, 16),
       bty = "n")
No description has been provided for this image