quirk.py

Created by vnap0v

Created on May 24, 2025

733 Bytes

This script demonstrates a quirk with the numpy implementation in Numworks. when tested with software version 23.2.6. When multiplying a int or float with a numpy array an error occurs if the int or float comes first. For example as in float * array. When the array comes first as in array * float the operation works fine. Also converting the int or float to a one element array eliminates the error.


import numpy as np
#x is numpy ndarray
x=np.linspace(0,1,6)
#a is scalar, float in this case
a=3.5
#b is numpy ndarray of 1 element
b=np.array([3.0])
#runs ok
y=x*a;y2=x+a
print("scalar can be int of float")
print("ok: numpyarray * scalar")
print("ok: numpyarray + scalar")
y3=x*b;y4=b*x;y5=x+b;y6=b+x
print("ok: numpyarray * numpyarray")
print("ok: numpyarray + numpyarray")
#following operations give error
print("These operations give error")
print("on the Numworks calc:")
print("\"TypeError: Unsupported types\"")
try:
  z=a*x
except:
  print("error: scalar * numpyarray")
else:
  print("ok: scalar * numpyarray")
try:
  z2=a+x
except:
  print("error: scalar + numpyarray")
else:
  print("ok: scalar + numpyarray")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.