├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ └── build.yml ├── .gitignore ├── .readthedocs.yaml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── build-cp.sh ├── build.sh ├── build ├── esp32-cmake.sh ├── esp32.sh ├── rp2.sh └── rp2w.sh ├── code ├── micropython.cmake ├── micropython.mk ├── ndarray.c ├── ndarray.h ├── ndarray_operators.c ├── ndarray_operators.h ├── ndarray_properties.c ├── ndarray_properties.h ├── numpy │ ├── approx.c │ ├── approx.h │ ├── bitwise.c │ ├── bitwise.h │ ├── carray │ │ ├── carray.c │ │ ├── carray.h │ │ ├── carray_tools.c │ │ └── carray_tools.h │ ├── compare.c │ ├── compare.h │ ├── create.c │ ├── create.h │ ├── fft │ │ ├── fft.c │ │ ├── fft.h │ │ ├── fft_tools.c │ │ └── fft_tools.h │ ├── filter.c │ ├── filter.h │ ├── io │ │ ├── io.c │ │ └── io.h │ ├── linalg │ │ ├── linalg.c │ │ ├── linalg.h │ │ ├── linalg_tools.c │ │ └── linalg_tools.h │ ├── ndarray │ │ ├── ndarray_iter.c │ │ └── ndarray_iter.h │ ├── numerical.c │ ├── numerical.h │ ├── numpy.c │ ├── numpy.h │ ├── poly.c │ ├── poly.h │ ├── random │ │ ├── random.c │ │ └── random.h │ ├── stats.c │ ├── stats.h │ ├── transform.c │ ├── transform.h │ ├── vector.c │ └── vector.h ├── scipy │ ├── integrate │ │ ├── integrate.c │ │ └── integrate.h │ ├── linalg │ │ ├── linalg.c │ │ └── linalg.h │ ├── optimize │ │ ├── optimize.c │ │ └── optimize.h │ ├── scipy.c │ ├── scipy.h │ ├── signal │ │ ├── signal.c │ │ └── signal.h │ └── special │ │ ├── special.c │ │ └── special.h ├── ulab.c ├── ulab.h ├── ulab_tools.c ├── ulab_tools.h ├── user │ ├── user.c │ └── user.h └── utils │ ├── utils.c │ └── utils.h ├── docs ├── manual │ ├── Makefile │ ├── make.bat │ └── source │ │ ├── conf.py │ │ ├── index.rst │ │ ├── numpy-fft.rst │ │ ├── numpy-functions.rst │ │ ├── numpy-linalg.rst │ │ ├── numpy-random.rst │ │ ├── numpy-universal.rst │ │ ├── scipy-integrate.rst │ │ ├── scipy-linalg.rst │ │ ├── scipy-optimize.rst │ │ ├── scipy-signal.rst │ │ ├── scipy-special.rst │ │ ├── ulab-intro.rst │ │ ├── ulab-ndarray.rst │ │ ├── ulab-programming.rst │ │ ├── ulab-tricks.rst │ │ └── ulab-utils.rst ├── numpy-fft.ipynb ├── numpy-functions.ipynb ├── numpy-linalg.ipynb ├── numpy-random.ipynb ├── numpy-universal.ipynb ├── scipy-integrate.ipynb ├── scipy-linalg.ipynb ├── scipy-optimize.ipynb ├── scipy-signal.ipynb ├── scipy-special.ipynb ├── templates │ ├── manual.tpl │ └── rst.tpl ├── ulab-approx.ipynb ├── ulab-change-log.md ├── ulab-compare.ipynb ├── ulab-convert.ipynb ├── ulab-intro.ipynb ├── ulab-ndarray.ipynb ├── ulab-numerical.ipynb ├── ulab-poly.ipynb ├── ulab-programming.ipynb ├── ulab-tricks.ipynb └── ulab-utils.ipynb ├── requirements.txt ├── requirements_cp_dev.txt ├── run-tests ├── snippets ├── json_to_ndarray.py ├── ndarray_to_json.py ├── numpy │ ├── __init__.py │ ├── core │ │ ├── __init__.py │ │ ├── fromnumeric.py │ │ ├── multiarray.py │ │ ├── numeric.py │ │ ├── overrides.py │ │ └── shape_base.py │ └── lib │ │ ├── __init__.py │ │ ├── block.py │ │ ├── function_base.py │ │ ├── polynomial.py │ │ └── type_check.py ├── rclass.py ├── scipy │ ├── __init__.py │ └── signal │ │ ├── __init__.py │ │ └── filter_design.py └── tests │ ├── numpy │ ├── core │ │ ├── fromnumeric.py │ │ ├── fromnumeric.py.exp │ │ ├── multiarray.py │ │ ├── multiarray.py.exp │ │ ├── numeric.py │ │ ├── numeric.py.exp │ │ ├── shape_base.py │ │ └── shape_base.py.exp │ └── lib │ │ ├── block.py │ │ ├── block.py.exp │ │ ├── function_base.py │ │ ├── function_base.py.exp │ │ ├── polynomial.py │ │ ├── polynomial.py.exp │ │ ├── type_check.py │ │ └── type_check.py.exp │ └── scipy │ └── signal │ ├── filter_design.py │ └── filter_design.py.exp ├── test-common.sh ├── test-snippets.sh └── tests ├── 1d ├── complex │ ├── complex_exp.py │ ├── complex_exp.py.exp │ ├── complex_sqrt.py │ ├── complex_sqrt.py.exp │ ├── imag_real.py │ └── imag_real.py.exp └── numpy │ ├── 00smoke.py │ ├── 00smoke.py.exp │ ├── argminmax.py │ ├── argminmax.py.exp │ ├── compare.py │ ├── compare.py.exp │ ├── convolve.py │ ├── convolve.py.exp │ ├── fft.py │ ├── fft.py.exp │ ├── gc.py │ ├── gc.py.exp │ ├── interp.py │ ├── interp.py.exp │ ├── optimize.py │ ├── optimize.py.exp │ ├── poly.py │ ├── poly.py.exp │ ├── slicing.py │ ├── slicing.py.exp │ ├── slicing2.py │ ├── slicing2.py.exp │ ├── sum.py │ ├── sum.py.exp │ ├── trapz.py │ ├── trapz.py.exp │ ├── universal_functions.py │ └── universal_functions.py.exp ├── 2d ├── complex │ ├── binary_op.py │ ├── binary_op.py.exp │ ├── complex_exp.py │ ├── complex_exp.py.exp │ ├── complex_sqrt.py │ ├── complex_sqrt.py.exp │ ├── conjugate.py │ ├── conjugate.py.exp │ ├── imag_real.py │ ├── imag_real.py.exp │ ├── sort_complex.py │ └── sort_complex.py.exp ├── numpy │ ├── 00smoke.py │ ├── 00smoke.py.exp │ ├── and.py │ ├── and.py.exp │ ├── any_all.py │ ├── any_all.py.exp │ ├── arange.py │ ├── arange.py.exp │ ├── asarray.py │ ├── asarray.py.exp │ ├── bitwise_and.py │ ├── bitwise_and.py.exp │ ├── bitwise_or.py │ ├── bitwise_or.py.exp │ ├── bitwise_xor.py │ ├── bitwise_xor.py.exp │ ├── buffer.py │ ├── buffer.py.exp │ ├── cholesky.py │ ├── cholesky.py.exp │ ├── concatenate.py │ ├── concatenate.py.exp │ ├── delete.py │ ├── delete.py.exp │ ├── diag.py │ ├── diag.py.exp │ ├── eye.py │ ├── eye.py.exp │ ├── full.py │ ├── full.py.exp │ ├── initialisation.py │ ├── initialisation.py.exp │ ├── isinf.py │ ├── isinf.py.exp │ ├── left_shift.py │ ├── left_shift.py.exp │ ├── linalg.py │ ├── linalg.py.exp │ ├── linspace.py │ ├── linspace.py.exp │ ├── load_save.py │ ├── load_save.py.exp │ ├── loadtxt.py │ ├── loadtxt.py.exp │ ├── logspace.py │ ├── logspace.py.exp │ ├── methods.py │ ├── methods.py.exp │ ├── nonzero.py │ ├── nonzero.py.exp │ ├── numericals.py │ ├── numericals.py.exp │ ├── ones.py │ ├── ones.py.exp │ ├── operators.py │ ├── operators.py.exp │ ├── or.py │ ├── or.py.exp │ ├── random.py │ ├── random.py.exp │ ├── reshape.py │ ├── reshape.py.exp │ ├── right_shift.py │ ├── right_shift.py.exp │ ├── savetxt.py │ ├── savetxt.py.exp │ ├── signal.py │ ├── signal.py.exp │ ├── size.py │ ├── size.py.exp │ ├── sort.py │ ├── sort.py.exp │ ├── sum.py │ ├── sum.py.exp │ ├── take.py │ ├── take.py.exp │ ├── vectorize.py │ ├── vectorize.py.exp │ ├── where.py │ ├── where.py.exp │ ├── xor.py │ ├── xor.py.exp │ ├── zeros.py │ └── zeros.py.exp ├── scipy │ ├── cho_solve.py │ ├── cho_solve.py.exp │ ├── integrate.py │ ├── integrate.py.exp │ ├── solve_triangular.py │ ├── solve_triangular.py.exp │ ├── sosfilt.py │ └── sosfilt.py.exp └── utils │ ├── from_buffer.py │ └── from_buffer.py.exp ├── 3d ├── complex │ ├── complex_exp.py │ ├── complex_exp.py.exp │ ├── complex_sqrt.py │ ├── complex_sqrt.py.exp │ ├── imag_real.py │ └── imag_real.py.exp └── numpy │ ├── create.py │ └── create.py.exp └── 4d ├── complex ├── complex_exp.py ├── complex_exp.py.exp ├── complex_sqrt.py ├── complex_sqrt.py.exp ├── imag_real.py └── imag_real.py.exp └── numpy ├── create.py └── create.py.exp /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "[BUG]" 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. Give the `ulab` version 12 | 13 | ```python 14 | import ulab 15 | print(ulab.__version__) 16 | ``` 17 | 18 | **To Reproduce** 19 | Describe the steps to reproduce the behavior. 20 | 21 | **Expected behavior** 22 | A clear and concise description of what you expected to happen. 23 | 24 | **Additional context** 25 | Add any other context that might help to locate the root of the problem. 26 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "[FEATURE REQUEST]" 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the solution you'd like** 11 | A clear and concise description of what you want to happen. If possible, link to the `numpy/scipy` function. 12 | 13 | **Additional context** 14 | Add any other context about the feature request here. 15 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build CI 2 | 3 | on: 4 | push: 5 | pull_request: 6 | paths: 7 | - 'code/**' 8 | - 'tests/**' 9 | - '.github/workflows/**' 10 | - 'build*.sh' 11 | - 'requirements*.txt' 12 | release: 13 | types: [published] 14 | check_suite: 15 | types: [rerequested] 16 | 17 | jobs: 18 | micropython: 19 | continue-on-error: true 20 | strategy: 21 | matrix: 22 | os: 23 | - ubuntu-20.04 24 | - macOS-latest 25 | dims: [1, 2, 3, 4] 26 | runs-on: ${{ matrix.os }} 27 | steps: 28 | - name: Dump GitHub context 29 | env: 30 | GITHUB_CONTEXT: ${{ toJson(github) }} 31 | run: echo "$GITHUB_CONTEXT" 32 | - name: Set up Python 3.12 33 | uses: actions/setup-python@v5 34 | with: 35 | python-version: "3.12" 36 | 37 | - name: Install requirements 38 | run: | 39 | if type -path apt-get; then 40 | sudo apt update && sudo apt-get install gcc-multilib 41 | fi 42 | 43 | - name: Versions 44 | run: | 45 | gcc --version 46 | python3 --version 47 | - name: Checkout ulab 48 | uses: actions/checkout@v4 49 | 50 | - name: Checkout micropython repo 51 | uses: actions/checkout@v4 52 | with: 53 | repository: micropython/micropython 54 | path: micropython 55 | 56 | - name: Run build.sh 57 | run: ./build.sh ${{ matrix.dims }} 58 | 59 | circuitpython: 60 | continue-on-error: true 61 | strategy: 62 | matrix: 63 | os: 64 | - ubuntu-20.04 65 | - macOS-latest 66 | dims: [1, 2, 3, 4] 67 | runs-on: ${{ matrix.os }} 68 | steps: 69 | - name: Dump GitHub context 70 | env: 71 | GITHUB_CONTEXT: ${{ toJson(github) }} 72 | run: echo "$GITHUB_CONTEXT" 73 | - name: Set up Python 3.12 74 | uses: actions/setup-python@v5 75 | with: 76 | python-version: "3.12" 77 | 78 | - name: Versions 79 | run: | 80 | gcc --version 81 | python3 --version 82 | 83 | - name: Checkout ulab 84 | uses: actions/checkout@v4 85 | 86 | - name: Install requirements 87 | run: | 88 | if type -path apt-get; then 89 | sudo apt update && sudo apt-get install gettext librsvg2-bin 90 | else 91 | brew install gettext librsvg 92 | echo >>$GITHUB_PATH /usr/local/opt/gettext/bin 93 | echo >>$GITHUB_PATH /usr/local/opt/librsvg/bin 94 | fi 95 | python3 -mpip install --upgrade -r requirements_cp_dev.txt 96 | 97 | - name: Run build-cp.sh 98 | run: ./build-cp.sh ${{ matrix.dims }} 99 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /micropython 2 | /circuitpython 3 | /*.exp 4 | /*.out 5 | /docs/manual/build/ 6 | /docs/manual/source/**/*.pyi 7 | /docs/.ipynb_checkpoints/ 8 | /docs/ulab-test.ipynb 9 | /code/.atom-build.yml 10 | build/micropython 11 | build/ulab 12 | 13 | .idea -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # .readthedocs.yaml 2 | # Read the Docs configuration file 3 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 4 | 5 | # Required 6 | version: 2 7 | 8 | # Set the version of Python and other tools you might need 9 | build: 10 | os: ubuntu-20.04 11 | tools: 12 | python: "3.9" 13 | 14 | # Build documentation in the docs/ directory with Sphinx 15 | sphinx: 16 | configuration: docs/manual/source/conf.py 17 | 18 | # If using Sphinx, optionally build your docs in additional formats such as PDF 19 | formats: all 20 | 21 | # Optionally declare the Python requirements required to build your docs 22 | python: 23 | install: 24 | - requirements: requirements.txt 25 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributions of any kind are always welcome. 2 | 3 | # Contributing to the code base 4 | 5 | If you feel like adding to the code, you can simply issue a pull request. If you do so, please, try to adhere to `micropython`'s [coding conventions](https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md#c-code-conventions). 6 | 7 | # Documentation 8 | 9 | However, you can also contribute to the documentation (preferably via the [jupyter notebooks](https://github.com/v923z/micropython-ulab/tree/master/docs). 10 | 11 | ## Testing 12 | 13 | If you decide to lend a hand with testing, here are the steps: 14 | 15 | 1. Write a test script that checks a particular function, or a set of related functions! 16 | 1. Drop this script in one of the folders in [ulab tests](https://github.com/v923z/micropython-ulab/tree/master/tests)! 17 | 1. Run the [./build.sh](https://github.com/v923z/micropython-ulab/blob/master/build.sh) script in the root directory of `ulab`! This will clone the latest `micropython`, compile the firmware for `unix`, execute all scripts in the `ulab/tests`, and compare the results to those in the expected results files, which are also in `ulab/tests`, and have an extension `.exp`. In case you have a new snippet, i.e., you have no expected results file, or if the results differ from those in the expected file, a new expected file will be generated in the root directory. You should inspect the contents of this file, and if they are satisfactory, then the file can be moved to the `ulab/tests` folder, alongside your snippet. 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Zoltán Vörös 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /build-cp.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | # POSIX compliant version 4 | readlinkf_posix() { 5 | [ "${1:-}" ] || return 1 6 | max_symlinks=40 7 | CDPATH='' # to avoid changing to an unexpected directory 8 | 9 | target=$1 10 | [ -e "${target%/}" ] || target=${1%"${1##*[!/]}"} # trim trailing slashes 11 | [ -d "${target:-/}" ] && target="$target/" 12 | 13 | cd -P . 2>/dev/null || return 1 14 | while [ "$max_symlinks" -ge 0 ] && max_symlinks=$((max_symlinks - 1)); do 15 | if [ ! "$target" = "${target%/*}" ]; then 16 | case $target in 17 | /*) cd -P "${target%/*}/" 2>/dev/null || break ;; 18 | *) cd -P "./${target%/*}" 2>/dev/null || break ;; 19 | esac 20 | target=${target##*/} 21 | fi 22 | 23 | if [ ! -L "$target" ]; then 24 | target="${PWD%/}${target:+/}${target}" 25 | printf '%s\n' "${target:-/}" 26 | return 0 27 | fi 28 | 29 | # `ls -dl` format: "%s %u %s %s %u %s %s -> %s\n", 30 | # , , , , 31 | # , , , 32 | # https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html 33 | link=$(ls -dl -- "$target" 2>/dev/null) || break 34 | target=${link#*" $target -> "} 35 | done 36 | return 1 37 | } 38 | NPROC=$(python3 -c 'import multiprocessing; print(multiprocessing.cpu_count())') 39 | HERE="$(dirname -- "$(readlinkf_posix -- "${0}")" )" 40 | [ -e circuitpython/py/py.mk ] || (git clone --branch main https://github.com/adafruit/circuitpython && cd circuitpython && make fetch-all-submodules && git submodule update --init lib/uzlib tools) 41 | rm -rf circuitpython/extmod/ulab; ln -s "$HERE" circuitpython/extmod/ulab 42 | dims=${1-2} 43 | make -C circuitpython/mpy-cross -j$NPROC 44 | make -k -C circuitpython/ports/unix -j$NPROC DEBUG=1 MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 MICROPY_SSL_AXTLS=0 MICROPY_PY_USSL=0 CFLAGS_EXTRA="-Wno-tautological-constant-out-of-range-compare -Wno-unknown-pragmas -DULAB_MAX_DIMS=$dims" BUILD=build-$dims PROG=micropython-$dims 45 | 46 | # bash test-common.sh "${dims}" "circuitpython/ports/unix/micropython-$dims" 47 | 48 | # Docs don't depend on the dimensionality, so only do it once 49 | if [ "$dims" -eq 2 ]; then 50 | (cd circuitpython && sphinx-build -E -W -b html . _build/html) 51 | (cd circuitpython && make check-stubs) 52 | fi 53 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | GIT_HASH=`git describe --abbrev=8 --always` 4 | 5 | # POSIX compliant version 6 | readlinkf_posix() { 7 | [ "${1:-}" ] || return 1 8 | max_symlinks=40 9 | CDPATH='' # to avoid changing to an unexpected directory 10 | 11 | target=$1 12 | [ -e "${target%/}" ] || target=${1%"${1##*[!/]}"} # trim trailing slashes 13 | [ -d "${target:-/}" ] && target="$target/" 14 | 15 | cd -P . 2>/dev/null || return 1 16 | while [ "$max_symlinks" -ge 0 ] && max_symlinks=$((max_symlinks - 1)); do 17 | if [ ! "$target" = "${target%/*}" ]; then 18 | case $target in 19 | /*) cd -P "${target%/*}/" 2>/dev/null || break ;; 20 | *) cd -P "./${target%/*}" 2>/dev/null || break ;; 21 | esac 22 | target=${target##*/} 23 | fi 24 | 25 | if [ ! -L "$target" ]; then 26 | target="${PWD%/}${target:+/}${target}" 27 | printf '%s\n' "${target:-/}" 28 | return 0 29 | fi 30 | 31 | # `ls -dl` format: "%s %u %s %s %u %s %s -> %s\n", 32 | # , , , , 33 | # , , , 34 | # https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html 35 | link=$(ls -dl -- "$target" 2>/dev/null) || break 36 | target=${link#*" $target -> "} 37 | done 38 | return 1 39 | } 40 | NPROC=`python3 -c 'import multiprocessing; print(multiprocessing.cpu_count())'` 41 | PLATFORM=`python3 -c 'import sys; print(sys.platform)'` 42 | set -e 43 | HERE="$(dirname -- "$(readlinkf_posix -- "${0}")" )" 44 | dims=${1-2} 45 | if [ ! -d "micropython" ] ; then 46 | git clone https://github.com/micropython/micropython 47 | else 48 | git -C micropython pull 49 | fi 50 | make -C micropython/mpy-cross -j${NPROC} 51 | make -C micropython/ports/unix submodules 52 | make -C micropython/ports/unix -j${NPROC} USER_C_MODULES="${HERE}" DEBUG=1 STRIP=: MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 CFLAGS_EXTRA=-DULAB_MAX_DIMS=$dims CFLAGS_EXTRA+=-DULAB_HASH=$GIT_HASH BUILD=build-$dims PROG=micropython-$dims 53 | 54 | PROG="micropython/ports/unix/build-$dims/micropython-$dims" 55 | if [ ! -e "$PROG" ]; then 56 | # Older MicroPython revision, executable is still in ports/unix. 57 | PROG="micropython/ports/unix/micropython-$dims" 58 | fi 59 | 60 | bash test-common.sh "${dims}" "$PROG" 61 | 62 | # Build with single-precision float. 63 | make -C micropython/ports/unix -j${NPROC} USER_C_MODULES="${HERE}" DEBUG=1 STRIP=: MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 CFLAGS_EXTRA=-DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_FLOAT CFLAGS_EXTRA+=-DULAB_MAX_DIMS=$dims CFLAGS_EXTRA+=-DULAB_HASH=$GIT_HASH BUILD=build-nanbox-$dims PROG=micropython-nanbox-$dims 64 | 65 | # The unix nanbox variant builds as a 32-bit executable and requires gcc-multilib. 66 | # macOS doesn't support i386 builds so only build on linux. 67 | if [ $PLATFORM = linux ]; then 68 | make -C micropython/ports/unix -j${NPROC} VARIANT=nanbox USER_C_MODULES="${HERE}" DEBUG=1 STRIP=: MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 CFLAGS_EXTRA=-DULAB_MAX_DIMS=$dims CFLAGS_EXTRA+=-DULAB_HASH=$GIT_HASH BUILD=build-nanbox-$dims PROG=micropython-nanbox-$dims 69 | fi 70 | 71 | -------------------------------------------------------------------------------- /build/esp32-cmake.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export BUILD_DIR=$(pwd) 4 | 5 | echo "--- CLONING ULAB ---" 6 | git clone --depth 1 https://github.com/v923z/micropython-ulab.git ulab 7 | 8 | echo "--- CLONING MICROPYTHON ---" 9 | git clone --depth 1 https://github.com/micropython/micropython.git 10 | 11 | echo "--- CLONING ESP-IDF ---" 12 | cd $BUILD_DIR/micropython/ 13 | git clone --depth 1 -b v4.0.2 --recursive https://github.com/espressif/esp-idf.git 14 | 15 | echo "--- INSTALL ESP-IDF ---" 16 | cd $BUILD_DIR/micropython/esp-idf 17 | ./install.sh 18 | . ./export.sh 19 | 20 | echo "--- MPY-CROSS ---" 21 | cd $BUILD_DIR/micropython/mpy-cross 22 | make 23 | 24 | echo "--- ESP32 SUBMODULES ---" 25 | cd $BUILD_DIR/micropython/ports/esp32 26 | make submodules 27 | 28 | echo "--- PATCH MAKEFILE ---" 29 | cp $BUILD_DIR/micropython/ports/esp32/Makefile $BUILD_DIR/micropython/ports/esp32/MakefileOld 30 | echo "BOARD = GENERIC" > $BUILD_DIR/micropython/ports/esp32/Makefile 31 | echo "USER_C_MODULES = \$(BUILD_DIR)/ulab/code/micropython.cmake" >> $BUILD_DIR/micropython/ports/esp32/Makefile 32 | cat $BUILD_DIR/micropython/ports/esp32/MakefileOld >> $BUILD_DIR/micropython/ports/esp32/Makefile 33 | 34 | echo "--- MAKE ---" 35 | make 36 | -------------------------------------------------------------------------------- /build/esp32.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export BUILD_DIR=$(pwd) 4 | 5 | git clone https://github.com/v923z/micropython-ulab.git ulab 6 | git clone https://github.com/micropython/micropython.git 7 | 8 | cd $BUILD_DIR/micropython/ 9 | git checkout tags/v1.14 10 | 11 | git submodule update --init 12 | cd ./mpy-cross && make # build cross-compiler (required) 13 | 14 | cd $BUILD_DIR/micropython/ports/esp32 15 | make ESPIDF= # will display supported ESP-IDF commit hashes 16 | # output should look like: """ 17 | # ... 18 | # Supported git hash (v3.3): 9e70825d1e1cbf7988cf36981774300066580ea7 19 | # Supported git hash (v4.0) (experimental): 4c81978a3e2220674a432a588292a4c860eef27b 20 | 21 | ESPIDF_VER=9e70825d1e1cbf7988cf36981774300066580ea7 22 | 23 | mkdir $BUILD_DIR/micropython/esp32 24 | 25 | cd $BUILD_DIR/micropython/esp32 26 | git clone https://github.com/espressif/esp-idf.git esp-idf 27 | cd $BUILD_DIR/micropython/esp32/esp-idf 28 | git checkout $ESPIDF_VER 29 | git submodule update --init --recursive # get idf submodules 30 | pip install -r ./requirements.txt # install python reqs 31 | 32 | curl https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar xvz 33 | 34 | cd $BUILD_DIR/micropython/ports/esp32 35 | # temporarily add esp32 compiler to path 36 | export PATH=$BUILD_DIR/micropython/esp32/esp-idf/xtensa-esp32-elf/bine:$PATH 37 | export ESPIDF=$BUILD_DIR/micropython/esp32/esp-idf 38 | export BOARD=GENERIC # board options are in ./board 39 | export USER_C_MODULES=$BUILD_DIR/ulab # include ulab in firmware 40 | 41 | make submodules & make all 42 | -------------------------------------------------------------------------------- /build/rp2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export BUILD_DIR=$(pwd) 4 | export MPY_DIR=$BUILD_DIR/micropython 5 | export ULAB_DIR=$BUILD_DIR/../code 6 | 7 | if [ ! -d $ULAB_DIR ]; then 8 | printf "Cloning ulab\n" 9 | ULAB_DIR=$BUILD_DIR/ulab/code 10 | git clone https://github.com/v923z/micropython-ulab.git ulab 11 | fi 12 | 13 | if [ ! -d $MPY_DIR ]; then 14 | printf "Cloning MicroPython\n" 15 | git clone https://github.com/micropython/micropython.git micropython 16 | fi 17 | 18 | cd $MPY_DIR 19 | git submodule update --init 20 | cd ./mpy-cross && make # build cross-compiler (required) 21 | 22 | cd $MPY_DIR/ports/rp2 23 | rm -r build 24 | make USER_C_MODULES=$ULAB_DIR/micropython.cmake 25 | -------------------------------------------------------------------------------- /build/rp2w.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export BOARD=RPI_PICO_W 4 | export BUILD_DIR=$(pwd) 5 | export MPY_DIR=$BUILD_DIR/micropython 6 | export ULAB_DIR=$BUILD_DIR/../code 7 | 8 | if [ ! -d $ULAB_DIR ]; then 9 | printf "Cloning ulab\n" 10 | ULAB_DIR=$BUILD_DIR/ulab/code 11 | git clone https://github.com/v923z/micropython-ulab.git ulab 12 | fi 13 | 14 | if [ ! -d $MPY_DIR ]; then 15 | printf "Cloning MicroPython\n" 16 | git clone https://github.com/micropython/micropython.git micropython 17 | fi 18 | 19 | cd $MPY_DIR 20 | git submodule update --init 21 | cd ./mpy-cross && make # build cross-compiler (required) 22 | 23 | cd $MPY_DIR/ports/rp2 24 | make BOARD=$BOARD clean 25 | make USER_C_MODULES=$ULAB_DIR/micropython.cmake BOARD=$BOARD 26 | -------------------------------------------------------------------------------- /code/micropython.cmake: -------------------------------------------------------------------------------- 1 | add_library(usermod_ulab INTERFACE) 2 | 3 | file(GLOB_RECURSE ULAB_SOURCES ${CMAKE_CURRENT_LIST_DIR}/*.c) 4 | 5 | target_sources(usermod_ulab INTERFACE 6 | ${ULAB_SOURCES} 7 | ) 8 | 9 | target_include_directories(usermod_ulab INTERFACE 10 | ${CMAKE_CURRENT_LIST_DIR} 11 | ) 12 | 13 | target_compile_definitions(usermod_ulab INTERFACE 14 | MODULE_ULAB_ENABLED=1 15 | ) 16 | 17 | target_link_libraries(usermod INTERFACE usermod_ulab) 18 | 19 | -------------------------------------------------------------------------------- /code/micropython.mk: -------------------------------------------------------------------------------- 1 | 2 | USERMODULES_DIR := $(USERMOD_DIR) 3 | 4 | # Add all C files to SRC_USERMOD. 5 | SRC_USERMOD += $(USERMODULES_DIR)/scipy/integrate/integrate.c 6 | SRC_USERMOD += $(USERMODULES_DIR)/scipy/linalg/linalg.c 7 | SRC_USERMOD += $(USERMODULES_DIR)/scipy/optimize/optimize.c 8 | SRC_USERMOD += $(USERMODULES_DIR)/scipy/signal/signal.c 9 | SRC_USERMOD += $(USERMODULES_DIR)/scipy/special/special.c 10 | SRC_USERMOD += $(USERMODULES_DIR)/ndarray_operators.c 11 | SRC_USERMOD += $(USERMODULES_DIR)/ulab_tools.c 12 | SRC_USERMOD += $(USERMODULES_DIR)/ndarray.c 13 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/ndarray/ndarray_iter.c 14 | SRC_USERMOD += $(USERMODULES_DIR)/ndarray_properties.c 15 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/approx.c 16 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/bitwise.c 17 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/compare.c 18 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/carray/carray.c 19 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/carray/carray_tools.c 20 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/create.c 21 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/fft/fft.c 22 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/fft/fft_tools.c 23 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/filter.c 24 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/io/io.c 25 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/linalg/linalg.c 26 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/linalg/linalg_tools.c 27 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/numerical.c 28 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/poly.c 29 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/random/random.c 30 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/stats.c 31 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/transform.c 32 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/vector.c 33 | 34 | SRC_USERMOD += $(USERMODULES_DIR)/numpy/numpy.c 35 | SRC_USERMOD += $(USERMODULES_DIR)/scipy/scipy.c 36 | SRC_USERMOD += $(USERMODULES_DIR)/user/user.c 37 | SRC_USERMOD += $(USERMODULES_DIR)/utils/utils.c 38 | SRC_USERMOD += $(USERMODULES_DIR)/ulab.c 39 | 40 | CFLAGS_USERMOD += -I$(USERMODULES_DIR) 41 | 42 | override CFLAGS_EXTRA += -DMODULE_ULAB_ENABLED=1 43 | -------------------------------------------------------------------------------- /code/ndarray_properties.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2021 Zoltán Vörös 10 | * 11 | */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "py/obj.h" 18 | #include "py/runtime.h" 19 | 20 | #include "ulab.h" 21 | #include "ndarray.h" 22 | #include "numpy/ndarray/ndarray_iter.h" 23 | #if ULAB_SUPPORTS_COMPLEX 24 | #include "numpy/carray/carray.h" 25 | #endif 26 | 27 | void ndarray_properties_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { 28 | if (dest[0] == MP_OBJ_NULL) { 29 | switch(attr) { 30 | #if NDARRAY_HAS_DTYPE 31 | case MP_QSTR_dtype: 32 | dest[0] = ndarray_dtype(self_in); 33 | break; 34 | #endif 35 | #if NDARRAY_HAS_FLATITER 36 | case MP_QSTR_flat: 37 | dest[0] = ndarray_flatiter_make_new(self_in); 38 | break; 39 | #endif 40 | #if NDARRAY_HAS_ITEMSIZE 41 | case MP_QSTR_itemsize: 42 | dest[0] = ndarray_itemsize(self_in); 43 | break; 44 | #endif 45 | #if NDARRAY_HAS_SHAPE 46 | case MP_QSTR_shape: 47 | dest[0] = ndarray_shape(self_in); 48 | break; 49 | #endif 50 | #if NDARRAY_HAS_SIZE 51 | case MP_QSTR_size: 52 | dest[0] = ndarray_size(self_in); 53 | break; 54 | #endif 55 | #if NDARRAY_HAS_STRIDES 56 | case MP_QSTR_strides: 57 | dest[0] = ndarray_strides(self_in); 58 | break; 59 | #endif 60 | #if NDARRAY_HAS_TRANSPOSE 61 | case MP_QSTR_T: 62 | dest[0] = ndarray_transpose(self_in); 63 | break; 64 | #endif 65 | #if ULAB_SUPPORTS_COMPLEX 66 | #if ULAB_NUMPY_HAS_IMAG 67 | case MP_QSTR_imag: 68 | dest[0] = carray_imag(self_in); 69 | break; 70 | #endif 71 | #if ULAB_NUMPY_HAS_IMAG 72 | case MP_QSTR_real: 73 | dest[0] = carray_real(self_in); 74 | break; 75 | #endif 76 | #endif /* ULAB_SUPPORTS_COMPLEX */ 77 | default: 78 | // forward to locals dict 79 | dest[1] = MP_OBJ_SENTINEL; 80 | break; 81 | } 82 | } else { 83 | if(dest[1]) { 84 | switch(attr) { 85 | #if ULAB_MAX_DIMS > 1 86 | #if NDARRAY_HAS_RESHAPE 87 | case MP_QSTR_shape: 88 | ndarray_reshape_core(self_in, dest[1], 1); 89 | break; 90 | #endif 91 | #endif 92 | default: 93 | return; 94 | break; 95 | } 96 | dest[0] = MP_OBJ_NULL; 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /code/ndarray_properties.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020 Jeff Epler for Adafruit Industries 10 | * 2020-2021 Zoltán Vörös 11 | */ 12 | 13 | #ifndef _NDARRAY_PROPERTIES_ 14 | #define _NDARRAY_PROPERTIES_ 15 | 16 | #include "py/runtime.h" 17 | #include "py/binary.h" 18 | #include "py/obj.h" 19 | #include "py/objarray.h" 20 | 21 | #include "ulab.h" 22 | #include "ndarray.h" 23 | #include "numpy/ndarray/ndarray_iter.h" 24 | 25 | void ndarray_properties_attr(mp_obj_t , qstr , mp_obj_t *); 26 | 27 | #if NDARRAY_HAS_DTYPE 28 | MP_DEFINE_CONST_FUN_OBJ_1(ndarray_dtype_obj, ndarray_dtype); 29 | #endif 30 | 31 | #if NDARRAY_HAS_FLATITER 32 | MP_DEFINE_CONST_FUN_OBJ_1(ndarray_flatiter_make_new_obj, ndarray_flatiter_make_new); 33 | #endif 34 | 35 | #if NDARRAY_HAS_ITEMSIZE 36 | MP_DEFINE_CONST_FUN_OBJ_1(ndarray_itemsize_obj, ndarray_itemsize); 37 | #endif 38 | 39 | #if NDARRAY_HAS_SHAPE 40 | MP_DEFINE_CONST_FUN_OBJ_1(ndarray_shape_obj, ndarray_shape); 41 | #endif 42 | 43 | #if NDARRAY_HAS_SIZE 44 | MP_DEFINE_CONST_FUN_OBJ_1(ndarray_size_obj, ndarray_size); 45 | #endif 46 | 47 | #if NDARRAY_HAS_STRIDES 48 | MP_DEFINE_CONST_FUN_OBJ_1(ndarray_strides_obj, ndarray_strides); 49 | #endif 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /code/numpy/approx.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020-2021 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _APPROX_ 13 | #define _APPROX_ 14 | 15 | #include "../ulab.h" 16 | #include "../ndarray.h" 17 | 18 | #define APPROX_EPS MICROPY_FLOAT_CONST(1.0e-4) 19 | #define APPROX_NONZDELTA MICROPY_FLOAT_CONST(0.05) 20 | #define APPROX_ZDELTA MICROPY_FLOAT_CONST(0.00025) 21 | #define APPROX_ALPHA MICROPY_FLOAT_CONST(1.0) 22 | #define APPROX_BETA MICROPY_FLOAT_CONST(2.0) 23 | #define APPROX_GAMMA MICROPY_FLOAT_CONST(0.5) 24 | #define APPROX_DELTA MICROPY_FLOAT_CONST(0.5) 25 | 26 | MP_DECLARE_CONST_FUN_OBJ_KW(approx_interp_obj); 27 | MP_DECLARE_CONST_FUN_OBJ_KW(approx_trapz_obj); 28 | 29 | #endif /* _APPROX_ */ 30 | -------------------------------------------------------------------------------- /code/numpy/bitwise.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2023 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _BITWISE_ 13 | #define _BITWISE_ 14 | 15 | #include "../ulab.h" 16 | #include "../ndarray.h" 17 | 18 | enum BITWISE_FUNCTION_TYPE { 19 | BITWISE_AND, 20 | BITWISE_OR, 21 | BITWISE_XOR, 22 | BITWISE_LEFT_SHIFT, 23 | BITWISE_RIGHT_SHIFT, 24 | }; 25 | 26 | MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_and_obj); 27 | MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_or_obj); 28 | MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_xor_obj); 29 | MP_DECLARE_CONST_FUN_OBJ_2(left_shift_obj); 30 | MP_DECLARE_CONST_FUN_OBJ_2(right_shift_obj); 31 | 32 | #endif /* _BITWISE_ */ 33 | -------------------------------------------------------------------------------- /code/numpy/carray/carray_tools.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2022 Zoltán Vörös 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include "py/obj.h" 16 | #include "py/runtime.h" 17 | #include "py/misc.h" 18 | 19 | #include "../../ulab.h" 20 | #include "../../ndarray.h" 21 | 22 | #if ULAB_SUPPORTS_COMPLEX 23 | 24 | void raise_complex_NotImplementedError(void) { 25 | mp_raise_NotImplementedError(MP_ERROR_TEXT("not implemented for complex dtype")); 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /code/numpy/carray/carray_tools.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2022 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _CARRAY_TOOLS_ 13 | #define _CARRAY_TOOLS_ 14 | 15 | void raise_complex_NotImplementedError(void); 16 | 17 | #if ULAB_SUPPORTS_COMPLEX 18 | #define NOT_IMPLEMENTED_FOR_COMPLEX() raise_complex_NotImplementedError(); 19 | #define COMPLEX_DTYPE_NOT_IMPLEMENTED(dtype) if((dtype) == NDARRAY_COMPLEX) raise_complex_NotImplementedError(); 20 | #else 21 | #define NOT_IMPLEMENTED_FOR_COMPLEX() // do nothing 22 | #define COMPLEX_DTYPE_NOT_IMPLEMENTED(dtype) // do nothing 23 | #endif 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /code/numpy/create.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2020 Jeff Epler for Adafruit Industries 9 | * 2019-2021 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _CREATE_ 13 | #define _CREATE_ 14 | 15 | #include "../ulab.h" 16 | #include "../ndarray.h" 17 | 18 | #if ULAB_NUMPY_HAS_ARANGE 19 | mp_obj_t create_arange(size_t , const mp_obj_t *, mp_map_t *); 20 | MP_DECLARE_CONST_FUN_OBJ_KW(create_arange_obj); 21 | #endif 22 | 23 | #if ULAB_NUMPY_HAS_ASARRAY 24 | mp_obj_t create_arange(size_t , const mp_obj_t *, mp_map_t *); 25 | MP_DECLARE_CONST_FUN_OBJ_KW(create_asarray_obj); 26 | #endif 27 | 28 | #if ULAB_NUMPY_HAS_CONCATENATE 29 | mp_obj_t create_concatenate(size_t , const mp_obj_t *, mp_map_t *); 30 | MP_DECLARE_CONST_FUN_OBJ_KW(create_concatenate_obj); 31 | #endif 32 | 33 | #if ULAB_NUMPY_HAS_DIAG 34 | mp_obj_t create_diag(size_t , const mp_obj_t *, mp_map_t *); 35 | MP_DECLARE_CONST_FUN_OBJ_KW(create_diag_obj); 36 | #endif 37 | 38 | #if ULAB_MAX_DIMS > 1 39 | #if ULAB_NUMPY_HAS_EYE 40 | mp_obj_t create_eye(size_t , const mp_obj_t *, mp_map_t *); 41 | MP_DECLARE_CONST_FUN_OBJ_KW(create_eye_obj); 42 | #endif 43 | #endif 44 | 45 | #if ULAB_NUMPY_HAS_FULL 46 | mp_obj_t create_full(size_t , const mp_obj_t *, mp_map_t *); 47 | MP_DECLARE_CONST_FUN_OBJ_KW(create_full_obj); 48 | #endif 49 | 50 | #if ULAB_NUMPY_HAS_LINSPACE 51 | mp_obj_t create_linspace(size_t , const mp_obj_t *, mp_map_t *); 52 | MP_DECLARE_CONST_FUN_OBJ_KW(create_linspace_obj); 53 | #endif 54 | 55 | #if ULAB_NUMPY_HAS_LOGSPACE 56 | mp_obj_t create_logspace(size_t , const mp_obj_t *, mp_map_t *); 57 | MP_DECLARE_CONST_FUN_OBJ_KW(create_logspace_obj); 58 | #endif 59 | 60 | #if ULAB_NUMPY_HAS_ONES 61 | mp_obj_t create_ones(size_t , const mp_obj_t *, mp_map_t *); 62 | MP_DECLARE_CONST_FUN_OBJ_KW(create_ones_obj); 63 | #endif 64 | 65 | #if ULAB_NUMPY_HAS_TAKE 66 | mp_obj_t create_take(size_t , const mp_obj_t *, mp_map_t *); 67 | MP_DECLARE_CONST_FUN_OBJ_KW(create_take_obj); 68 | #endif 69 | 70 | #if ULAB_NUMPY_HAS_ZEROS 71 | mp_obj_t create_zeros(size_t , const mp_obj_t *, mp_map_t *); 72 | MP_DECLARE_CONST_FUN_OBJ_KW(create_zeros_obj); 73 | #endif 74 | 75 | #if ULAB_NUMPY_HAS_FROMBUFFER 76 | mp_obj_t create_frombuffer(size_t , const mp_obj_t *, mp_map_t *); 77 | MP_DECLARE_CONST_FUN_OBJ_KW(create_frombuffer_obj); 78 | #endif 79 | 80 | #define ARANGE_LOOP(type_, ndarray, len, step, stop) \ 81 | ({\ 82 | type_ *array = (type_ *)(ndarray)->array;\ 83 | for (size_t i = 0; i < (len) - 1; i++, (value) += (step)) {\ 84 | *array++ = (type_)(value);\ 85 | }\ 86 | *array = (type_)(stop);\ 87 | }) 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /code/numpy/fft/fft.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2019-2021 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _FFT_ 13 | #define _FFT_ 14 | 15 | #include "../../ulab.h" 16 | #include "../../ulab_tools.h" 17 | #include "../../ndarray.h" 18 | #include "fft_tools.h" 19 | 20 | extern const mp_obj_module_t ulab_fft_module; 21 | 22 | #if ULAB_SUPPORTS_COMPLEX & ULAB_FFT_IS_NUMPY_COMPATIBLE 23 | MP_DECLARE_CONST_FUN_OBJ_3(fft_fft_obj); 24 | MP_DECLARE_CONST_FUN_OBJ_3(fft_ifft_obj); 25 | #else 26 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(fft_fft_obj); 27 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(fft_ifft_obj); 28 | #endif 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /code/numpy/fft/fft_tools.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2019-2021 Zoltán Vörös 9 | */ 10 | 11 | #ifndef _FFT_TOOLS_ 12 | #define _FFT_TOOLS_ 13 | 14 | enum FFT_TYPE { 15 | FFT_FFT, 16 | FFT_IFFT, 17 | }; 18 | 19 | #if ULAB_SUPPORTS_COMPLEX & ULAB_FFT_IS_NUMPY_COMPATIBLE 20 | void fft_kernel(mp_float_t *, size_t , int ); 21 | mp_obj_t fft_fft_ifft(mp_obj_t , uint8_t ); 22 | #else 23 | void fft_kernel(mp_float_t *, mp_float_t *, size_t , int ); 24 | mp_obj_t fft_fft_ifft(size_t , mp_obj_t , mp_obj_t , uint8_t ); 25 | #endif /* ULAB_SUPPORTS_COMPLEX & ULAB_FFT_IS_NUMPY_COMPATIBLE */ 26 | 27 | #endif /* _FFT_TOOLS_ */ 28 | -------------------------------------------------------------------------------- /code/numpy/filter.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020 Jeff Epler for Adafruit Industries 10 | * 2020-2021 Zoltán Vörös 11 | */ 12 | 13 | #ifndef _FILTER_ 14 | #define _FILTER_ 15 | 16 | #include "../ulab.h" 17 | #include "../ndarray.h" 18 | 19 | MP_DECLARE_CONST_FUN_OBJ_KW(filter_convolve_obj); 20 | #endif 21 | -------------------------------------------------------------------------------- /code/numpy/io/io.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2022 Zoltán Vörös 9 | */ 10 | 11 | #ifndef _ULAB_IO_ 12 | #define _ULAB_IO_ 13 | 14 | MP_DECLARE_CONST_FUN_OBJ_1(io_load_obj); 15 | MP_DECLARE_CONST_FUN_OBJ_KW(io_loadtxt_obj); 16 | MP_DECLARE_CONST_FUN_OBJ_2(io_save_obj); 17 | MP_DECLARE_CONST_FUN_OBJ_KW(io_savetxt_obj); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /code/numpy/linalg/linalg.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2019-2021 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _LINALG_ 13 | #define _LINALG_ 14 | 15 | #include "../../ulab.h" 16 | #include "../../ndarray.h" 17 | #include "linalg_tools.h" 18 | 19 | extern const mp_obj_module_t ulab_linalg_module; 20 | 21 | MP_DECLARE_CONST_FUN_OBJ_1(linalg_cholesky_obj); 22 | MP_DECLARE_CONST_FUN_OBJ_1(linalg_det_obj); 23 | MP_DECLARE_CONST_FUN_OBJ_1(linalg_eig_obj); 24 | MP_DECLARE_CONST_FUN_OBJ_1(linalg_inv_obj); 25 | MP_DECLARE_CONST_FUN_OBJ_KW(linalg_norm_obj); 26 | MP_DECLARE_CONST_FUN_OBJ_KW(linalg_qr_obj); 27 | #endif 28 | -------------------------------------------------------------------------------- /code/numpy/linalg/linalg_tools.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2019-2021 Zoltán Vörös 9 | */ 10 | 11 | #ifndef _TOOLS_TOOLS_ 12 | #define _TOOLS_TOOLS_ 13 | 14 | #ifndef LINALG_EPSILON 15 | #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 16 | #define LINALG_EPSILON MICROPY_FLOAT_CONST(1.2e-7) 17 | #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 18 | #define LINALG_EPSILON MICROPY_FLOAT_CONST(2.3e-16) 19 | #endif 20 | #endif /* LINALG_EPSILON */ 21 | 22 | #define JACOBI_MAX 20 23 | 24 | bool linalg_invert_matrix(mp_float_t *, size_t ); 25 | size_t linalg_jacobi_rotations(mp_float_t *, mp_float_t *, size_t ); 26 | 27 | #endif /* _TOOLS_TOOLS_ */ 28 | 29 | -------------------------------------------------------------------------------- /code/numpy/ndarray/ndarray_iter.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2021 Zoltán Vörös 10 | * 11 | */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "py/obj.h" 18 | #include "py/runtime.h" 19 | 20 | #include "ndarray_iter.h" 21 | 22 | #ifdef NDARRAY_HAS_FLATITER 23 | mp_obj_t ndarray_flatiter_make_new(mp_obj_t self_in) { 24 | ndarray_flatiter_t *flatiter = m_new_obj(ndarray_flatiter_t); 25 | flatiter->base.type = &ndarray_flatiter_type; 26 | flatiter->iternext = ndarray_flatiter_next; 27 | flatiter->ndarray = self_in; 28 | flatiter->cur = 0; 29 | return MP_OBJ_FROM_PTR(flatiter); 30 | } 31 | 32 | mp_obj_t ndarray_flatiter_next(mp_obj_t self_in) { 33 | ndarray_flatiter_t *self = MP_OBJ_TO_PTR(self_in); 34 | ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(self->ndarray); 35 | uint8_t *array = (uint8_t *)ndarray->array; 36 | 37 | if(self->cur < ndarray->len) { 38 | uint32_t remainder = self->cur; 39 | uint8_t i = ULAB_MAX_DIMS - 1; 40 | do { 41 | size_t div = (remainder / ndarray->shape[i]); 42 | array += remainder * ndarray->strides[i]; 43 | remainder -= div * ndarray->shape[i]; 44 | i--; 45 | } while(i > ULAB_MAX_DIMS - ndarray->ndim); 46 | self->cur++; 47 | return ndarray_get_item(ndarray, array); 48 | } 49 | return MP_OBJ_STOP_ITERATION; 50 | } 51 | 52 | mp_obj_t ndarray_new_flatiterator(mp_obj_t flatiter_in, mp_obj_iter_buf_t *iter_buf) { 53 | assert(sizeof(ndarray_flatiter_t) <= sizeof(mp_obj_iter_buf_t)); 54 | ndarray_flatiter_t *iter = (ndarray_flatiter_t *)iter_buf; 55 | ndarray_flatiter_t *flatiter = MP_OBJ_TO_PTR(flatiter_in); 56 | iter->base.type = &mp_type_polymorph_iter; 57 | iter->iternext = ndarray_flatiter_next; 58 | iter->ndarray = flatiter->ndarray; 59 | iter->cur = 0; 60 | return MP_OBJ_FROM_PTR(iter); 61 | } 62 | 63 | mp_obj_t ndarray_get_flatiterator(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { 64 | return ndarray_new_flatiterator(o_in, iter_buf); 65 | } 66 | #endif /* NDARRAY_HAS_FLATITER */ 67 | -------------------------------------------------------------------------------- /code/numpy/ndarray/ndarray_iter.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020 Jeff Epler for Adafruit Industries 10 | * 2020-2021 Zoltán Vörös 11 | */ 12 | 13 | #ifndef _NDARRAY_ITER_ 14 | #define _NDARRAY_ITER_ 15 | 16 | #include "py/runtime.h" 17 | #include "py/binary.h" 18 | #include "py/obj.h" 19 | #include "py/objarray.h" 20 | 21 | #include "../../ulab.h" 22 | #include "../../ndarray.h" 23 | 24 | // TODO: take simply mp_obj_ndarray_it_t from ndarray.c 25 | typedef struct _mp_obj_ndarray_flatiter_t { 26 | mp_obj_base_t base; 27 | mp_fun_1_t iternext; 28 | mp_obj_t ndarray; 29 | size_t cur; 30 | } ndarray_flatiter_t; 31 | 32 | mp_obj_t ndarray_get_flatiterator(mp_obj_t , mp_obj_iter_buf_t *); 33 | mp_obj_t ndarray_flatiter_make_new(mp_obj_t ); 34 | mp_obj_t ndarray_flatiter_next(mp_obj_t ); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /code/numpy/numpy.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020-2021 Zoltán Vörös 10 | * 11 | */ 12 | 13 | #ifndef _NUMPY_ 14 | #define _NUMPY_ 15 | 16 | #include "../ulab.h" 17 | #include "../ndarray.h" 18 | 19 | extern const mp_obj_module_t ulab_numpy_module; 20 | 21 | #endif /* _NUMPY_ */ 22 | -------------------------------------------------------------------------------- /code/numpy/poly.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2019-2021 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _POLY_ 13 | #define _POLY_ 14 | 15 | #include "../ulab.h" 16 | #include "../ndarray.h" 17 | 18 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(poly_polyfit_obj); 19 | MP_DECLARE_CONST_FUN_OBJ_2(poly_polyval_obj); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /code/numpy/random/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2024 Zoltán Vörös 9 | */ 10 | 11 | #include "../../ndarray.h" 12 | 13 | #ifndef _NUMPY_RANDOM_ 14 | #define _NUMPY_RANDOM_ 15 | 16 | 17 | #define PCG_MULTIPLIER_64 6364136223846793005ULL 18 | #define PCG_INCREMENT_64 1442695040888963407ULL 19 | 20 | extern const mp_obj_module_t ulab_numpy_random_module; 21 | 22 | extern const mp_obj_type_t random_generator_type; 23 | 24 | typedef struct _random_generator_obj_t { 25 | mp_obj_base_t base; 26 | uint64_t state; 27 | } random_generator_obj_t; 28 | 29 | mp_obj_t random_generator_make_new(const mp_obj_type_t *, size_t , size_t , const mp_obj_t *); 30 | void random_generator_print(const mp_print_t *, mp_obj_t , mp_print_kind_t ); 31 | 32 | 33 | MP_DECLARE_CONST_FUN_OBJ_KW(random_normal_obj); 34 | MP_DECLARE_CONST_FUN_OBJ_KW(random_random_obj); 35 | MP_DECLARE_CONST_FUN_OBJ_KW(random_uniform_obj); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /code/numpy/stats.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2019-2021 Zoltán Vörös 9 | * 2020 Scott Shawcroft for Adafruit Industries 10 | * 2020 Roberto Colistete Jr. 11 | * 2020 Taku Fukada 12 | * 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include "py/obj.h" 19 | #include "py/runtime.h" 20 | #include "py/misc.h" 21 | 22 | #include "../ulab.h" 23 | #include "../ulab_tools.h" 24 | #include "carray/carray_tools.h" 25 | #include "stats.h" 26 | 27 | #if ULAB_MAX_DIMS > 1 28 | #if ULAB_NUMPY_HAS_TRACE 29 | 30 | //| def trace(m: ulab.numpy.ndarray) -> _float: 31 | //| """ 32 | //| :param m: a square matrix 33 | //| 34 | //| Compute the trace of the matrix, the sum of its diagonal elements.""" 35 | //| ... 36 | //| 37 | 38 | static mp_obj_t stats_trace(mp_obj_t oin) { 39 | ndarray_obj_t *ndarray = tools_object_is_square(oin); 40 | COMPLEX_DTYPE_NOT_IMPLEMENTED(ndarray->dtype) 41 | mp_float_t trace = 0.0; 42 | for(size_t i=0; i < ndarray->shape[ULAB_MAX_DIMS - 1]; i++) { 43 | int32_t pos = i * (ndarray->strides[ULAB_MAX_DIMS - 1] + ndarray->strides[ULAB_MAX_DIMS - 2]); 44 | trace += ndarray_get_float_index(ndarray->array, ndarray->dtype, pos/ndarray->itemsize); 45 | } 46 | if(ndarray->dtype == NDARRAY_FLOAT) { 47 | return mp_obj_new_float(trace); 48 | } 49 | return mp_obj_new_int_from_float(trace); 50 | } 51 | 52 | MP_DEFINE_CONST_FUN_OBJ_1(stats_trace_obj, stats_trace); 53 | #endif 54 | #endif 55 | -------------------------------------------------------------------------------- /code/numpy/stats.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2019-2021 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _STATS_ 13 | #define _STATS_ 14 | 15 | #include "../ulab.h" 16 | #include "../ndarray.h" 17 | 18 | MP_DECLARE_CONST_FUN_OBJ_1(stats_trace_obj); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /code/numpy/transform.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2019-2021 Zoltán Vörös 9 | * 10 | */ 11 | 12 | #ifndef _TRANSFORM_ 13 | #define _TRANSFORM_ 14 | 15 | #include 16 | #include 17 | #include 18 | #include "py/obj.h" 19 | #include "py/runtime.h" 20 | #include "py/misc.h" 21 | 22 | #include "../ulab.h" 23 | #include "../ulab_tools.h" 24 | 25 | MP_DECLARE_CONST_FUN_OBJ_KW(transform_compress_obj); 26 | MP_DECLARE_CONST_FUN_OBJ_KW(transform_delete_obj); 27 | MP_DECLARE_CONST_FUN_OBJ_2(transform_dot_obj); 28 | MP_DECLARE_CONST_FUN_OBJ_KW(transform_size_obj); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /code/scipy/integrate/integrate.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2024 Harald Milz 10 | * 11 | */ 12 | 13 | #ifndef _SCIPY_INTEGRATE_ 14 | #define _SCIPY_INTEGRATE_ 15 | 16 | #include "../../ulab_tools.h" 17 | 18 | extern const mp_obj_module_t ulab_scipy_integrate_module; 19 | 20 | #if ULAB_INTEGRATE_HAS_TANHSINH 21 | MP_DECLARE_CONST_FUN_OBJ_KW(optimize_tanhsinh_obj); 22 | #endif 23 | #if ULAB_INTEGRATE_HAS_ROMBERG 24 | MP_DECLARE_CONST_FUN_OBJ_KW(optimize_romberg_obj); 25 | #endif 26 | #if ULAB_INTEGRATE_HAS_SIMPSON 27 | MP_DECLARE_CONST_FUN_OBJ_KW(optimize_simpson_obj); 28 | #endif 29 | #if ULAB_INTEGRATE_HAS_QUAD 30 | MP_DECLARE_CONST_FUN_OBJ_KW(optimize_quad_obj); 31 | #endif 32 | 33 | #endif /* _SCIPY_INTEGRATE_ */ 34 | 35 | -------------------------------------------------------------------------------- /code/scipy/linalg/linalg.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2021 Vikas Udupa 10 | * 11 | */ 12 | 13 | #ifndef _SCIPY_LINALG_ 14 | #define _SCIPY_LINALG_ 15 | 16 | extern const mp_obj_module_t ulab_scipy_linalg_module; 17 | 18 | MP_DECLARE_CONST_FUN_OBJ_KW(linalg_solve_triangular_obj); 19 | MP_DECLARE_CONST_FUN_OBJ_2(linalg_cho_solve_obj); 20 | 21 | #endif /* _SCIPY_LINALG_ */ 22 | -------------------------------------------------------------------------------- /code/scipy/optimize/optimize.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020-2021 Zoltán Vörös 10 | * 11 | */ 12 | 13 | #ifndef _SCIPY_OPTIMIZE_ 14 | #define _SCIPY_OPTIMIZE_ 15 | 16 | #include "../../ulab_tools.h" 17 | 18 | #ifndef OPTIMIZE_EPSILON 19 | #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 20 | #define OPTIMIZE_EPSILON MICROPY_FLOAT_CONST(1.2e-7) 21 | #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 22 | #define OPTIMIZE_EPSILON MICROPY_FLOAT_CONST(2.3e-16) 23 | #endif 24 | #endif 25 | 26 | #define OPTIMIZE_EPS MICROPY_FLOAT_CONST(1.0e-4) 27 | #define OPTIMIZE_NONZDELTA MICROPY_FLOAT_CONST(0.05) 28 | #define OPTIMIZE_ZDELTA MICROPY_FLOAT_CONST(0.00025) 29 | #define OPTIMIZE_ALPHA MICROPY_FLOAT_CONST(1.0) 30 | #define OPTIMIZE_BETA MICROPY_FLOAT_CONST(2.0) 31 | #define OPTIMIZE_GAMMA MICROPY_FLOAT_CONST(0.5) 32 | #define OPTIMIZE_DELTA MICROPY_FLOAT_CONST(0.5) 33 | 34 | extern const mp_obj_module_t ulab_scipy_optimize_module; 35 | 36 | MP_DECLARE_CONST_FUN_OBJ_KW(optimize_bisect_obj); 37 | MP_DECLARE_CONST_FUN_OBJ_KW(optimize_curve_fit_obj); 38 | MP_DECLARE_CONST_FUN_OBJ_KW(optimize_fmin_obj); 39 | MP_DECLARE_CONST_FUN_OBJ_KW(optimize_newton_obj); 40 | 41 | #endif /* _SCIPY_OPTIMIZE_ */ 42 | -------------------------------------------------------------------------------- /code/scipy/scipy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2020 Jeff Epler for Adafruit Industries 9 | * 2020 Scott Shawcroft for Adafruit Industries 10 | * 2020-2021 Zoltán Vörös 11 | * 2020 Taku Fukada 12 | */ 13 | 14 | #include 15 | #include "py/runtime.h" 16 | 17 | #include "../ulab.h" 18 | #include "optimize/optimize.h" 19 | #include "signal/signal.h" 20 | #include "special/special.h" 21 | #include "linalg/linalg.h" 22 | #include "integrate/integrate.h" 23 | 24 | 25 | #if ULAB_HAS_SCIPY 26 | 27 | //| """Compatibility layer for scipy""" 28 | //| 29 | 30 | static const mp_rom_map_elem_t ulab_scipy_globals_table[] = { 31 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_scipy) }, 32 | #if ULAB_SCIPY_HAS_INTEGRATE_MODULE 33 | { MP_ROM_QSTR(MP_QSTR_integrate), MP_ROM_PTR(&ulab_scipy_integrate_module) }, 34 | #endif 35 | #if ULAB_SCIPY_HAS_LINALG_MODULE 36 | { MP_ROM_QSTR(MP_QSTR_linalg), MP_ROM_PTR(&ulab_scipy_linalg_module) }, 37 | #endif 38 | #if ULAB_SCIPY_HAS_OPTIMIZE_MODULE 39 | { MP_ROM_QSTR(MP_QSTR_optimize), MP_ROM_PTR(&ulab_scipy_optimize_module) }, 40 | #endif 41 | #if ULAB_SCIPY_HAS_SIGNAL_MODULE 42 | { MP_ROM_QSTR(MP_QSTR_signal), MP_ROM_PTR(&ulab_scipy_signal_module) }, 43 | #endif 44 | #if ULAB_SCIPY_HAS_SPECIAL_MODULE 45 | { MP_ROM_QSTR(MP_QSTR_special), MP_ROM_PTR(&ulab_scipy_special_module) }, 46 | #endif 47 | }; 48 | 49 | static MP_DEFINE_CONST_DICT(mp_module_ulab_scipy_globals, ulab_scipy_globals_table); 50 | 51 | const mp_obj_module_t ulab_scipy_module = { 52 | .base = { &mp_type_module }, 53 | .globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_globals, 54 | }; 55 | #if CIRCUITPY_ULAB 56 | MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy, ulab_scipy_module); 57 | #endif 58 | #endif /* ULAB_HAS_SCIPY */ 59 | -------------------------------------------------------------------------------- /code/scipy/scipy.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020-2021 Zoltán Vörös 10 | * 11 | */ 12 | 13 | #ifndef _SCIPY_ 14 | #define _SCIPY_ 15 | 16 | #include "../ulab.h" 17 | #include "../ndarray.h" 18 | 19 | extern const mp_obj_module_t ulab_scipy_module; 20 | 21 | #endif /* _SCIPY_ */ 22 | -------------------------------------------------------------------------------- /code/scipy/signal/signal.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020-2021 Zoltán Vörös 10 | * 11 | */ 12 | 13 | #ifndef _SCIPY_SIGNAL_ 14 | #define _SCIPY_SIGNAL_ 15 | 16 | #include "../../ulab.h" 17 | #include "../../ndarray.h" 18 | 19 | extern const mp_obj_module_t ulab_scipy_signal_module; 20 | 21 | MP_DECLARE_CONST_FUN_OBJ_KW(signal_sosfilt_obj); 22 | 23 | #endif /* _SCIPY_SIGNAL_ */ 24 | -------------------------------------------------------------------------------- /code/scipy/special/special.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020 Jeff Epler for Adafruit Industries 10 | * 2020 Scott Shawcroft for Adafruit Industries 11 | * 2020-2021 Zoltán Vörös 12 | * 2020 Taku Fukada 13 | */ 14 | 15 | #include 16 | #include "py/runtime.h" 17 | 18 | #include "../../ulab.h" 19 | #include "../../numpy/vector.h" 20 | 21 | static const mp_rom_map_elem_t ulab_scipy_special_globals_table[] = { 22 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_special) }, 23 | #if ULAB_SCIPY_SPECIAL_HAS_ERF 24 | { MP_ROM_QSTR(MP_QSTR_erf), MP_ROM_PTR(&vector_erf_obj) }, 25 | #endif 26 | #if ULAB_SCIPY_SPECIAL_HAS_ERFC 27 | { MP_ROM_QSTR(MP_QSTR_erfc), MP_ROM_PTR(&vector_erfc_obj) }, 28 | #endif 29 | #if ULAB_SCIPY_SPECIAL_HAS_GAMMA 30 | { MP_ROM_QSTR(MP_QSTR_gamma), MP_ROM_PTR(&vector_gamma_obj) }, 31 | #endif 32 | #if ULAB_SCIPY_SPECIAL_HAS_GAMMALN 33 | { MP_ROM_QSTR(MP_QSTR_gammaln), MP_ROM_PTR(&vector_lgamma_obj) }, 34 | #endif 35 | }; 36 | 37 | static MP_DEFINE_CONST_DICT(mp_module_ulab_scipy_special_globals, ulab_scipy_special_globals_table); 38 | 39 | const mp_obj_module_t ulab_scipy_special_module = { 40 | .base = { &mp_type_module }, 41 | .globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_special_globals, 42 | }; 43 | #if CIRCUITPY_ULAB 44 | MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_special, ulab_scipy_special_module); 45 | #endif 46 | -------------------------------------------------------------------------------- /code/scipy/special/special.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020-2021 Zoltán Vörös 10 | * 11 | */ 12 | 13 | #ifndef _SCIPY_SPECIAL_ 14 | #define _SCIPY_SPECIAL_ 15 | 16 | #include "../../ulab.h" 17 | #include "../../ndarray.h" 18 | 19 | extern const mp_obj_module_t ulab_scipy_special_module; 20 | 21 | #endif /* _SCIPY_SPECIAL_ */ 22 | -------------------------------------------------------------------------------- /code/ulab_tools.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2020-2022 Zoltán Vörös 9 | */ 10 | 11 | #ifndef _TOOLS_ 12 | #define _TOOLS_ 13 | 14 | #include "ndarray.h" 15 | 16 | #define SWAP(t, a, b) { t tmp = a; a = b; b = tmp; } 17 | 18 | typedef struct _shape_strides_t { 19 | uint8_t increment; 20 | uint8_t axis; 21 | uint8_t ndim; 22 | size_t *shape; 23 | int32_t *strides; 24 | } shape_strides; 25 | 26 | mp_float_t ndarray_get_float_uint8(void *); 27 | mp_float_t ndarray_get_float_int8(void *); 28 | mp_float_t ndarray_get_float_uint16(void *); 29 | mp_float_t ndarray_get_float_int16(void *); 30 | mp_float_t ndarray_get_float_float(void *); 31 | void *ndarray_get_float_function(uint8_t ); 32 | 33 | uint8_t ndarray_upcast_dtype(uint8_t , uint8_t ); 34 | void *ndarray_set_float_function(uint8_t ); 35 | 36 | shape_strides tools_reduce_axes(ndarray_obj_t *, mp_obj_t ); 37 | int8_t tools_get_axis(mp_obj_t , uint8_t ); 38 | mp_obj_t ulab_tools_restore_dims(ndarray_obj_t * , ndarray_obj_t * , mp_obj_t , shape_strides ); 39 | ndarray_obj_t *tools_object_is_square(mp_obj_t ); 40 | 41 | uint8_t ulab_binary_get_size(uint8_t ); 42 | 43 | #if ULAB_SUPPORTS_COMPLEX 44 | void ulab_rescale_float_strides(int32_t *); 45 | #endif 46 | 47 | bool ulab_tools_mp_obj_is_scalar(mp_obj_t ); 48 | 49 | ndarray_obj_t *ulab_tools_inspect_out(mp_obj_t , uint8_t , uint8_t , size_t *, bool ); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /code/user/user.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * This file is part of the micropython-ulab project, 4 | * 5 | * https://github.com/v923z/micropython-ulab 6 | * 7 | * The MIT License (MIT) 8 | * 9 | * Copyright (c) 2020-2021 Zoltán Vörös 10 | */ 11 | 12 | #ifndef _USER_ 13 | #define _USER_ 14 | 15 | #include "../ulab.h" 16 | #include "../ndarray.h" 17 | 18 | extern const mp_obj_module_t ulab_user_module; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /code/utils/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the micropython-ulab project, 3 | * 4 | * https://github.com/v923z/micropython-ulab 5 | * 6 | * The MIT License (MIT) 7 | * 8 | * Copyright (c) 2020-2021 Zoltán Vörös 9 | */ 10 | 11 | #ifndef _UTILS_ 12 | #define _UTILS_ 13 | 14 | #include "../ulab.h" 15 | #include "../ndarray.h" 16 | 17 | extern const mp_obj_module_t ulab_utils_module; 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /docs/manual/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line, and also 5 | # from the environment for the first two. 6 | SPHINXOPTS ?= 7 | SPHINXBUILD ?= sphinx-build 8 | SOURCEDIR = source 9 | BUILDDIR = build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | clean: 18 | rm -rf "$(BUILDDIR)" 19 | 20 | 21 | # Catch-all target: route all unknown targets to Sphinx using the new 22 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 23 | %: Makefile 24 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 25 | -------------------------------------------------------------------------------- /docs/manual/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=source 11 | set BUILDDIR=build 12 | 13 | if "%1" == "" goto help 14 | 15 | %SPHINXBUILD% >NUL 2>NUL 16 | if errorlevel 9009 ( 17 | echo. 18 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 19 | echo.installed, then set the SPHINXBUILD environment variable to point 20 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 21 | echo.may add the Sphinx directory to PATH. 22 | echo. 23 | echo.If you don't have Sphinx installed, grab it from 24 | echo.http://sphinx-doc.org/ 25 | exit /b 1 26 | ) 27 | 28 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 29 | goto end 30 | 31 | :help 32 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 33 | 34 | :end 35 | popd 36 | -------------------------------------------------------------------------------- /docs/manual/source/index.rst: -------------------------------------------------------------------------------- 1 | 2 | .. ulab-manual documentation master file, created by 3 | sphinx-quickstart on Sat Oct 19 12:48:00 2019. 4 | You can adapt this file completely to your liking, but it should at least 5 | contain the root `toctree` directive. 6 | 7 | Welcome to the ulab book! 8 | ======================================= 9 | 10 | .. toctree:: 11 | :maxdepth: 2 12 | :caption: Introduction 13 | 14 | ulab-intro 15 | 16 | .. toctree:: 17 | :maxdepth: 2 18 | :caption: User's guide: 19 | 20 | ulab-ndarray 21 | numpy-functions 22 | numpy-universal 23 | numpy-fft 24 | numpy-linalg 25 | numpy-random 26 | scipy-integrate 27 | scipy-linalg 28 | scipy-optimize 29 | scipy-signal 30 | scipy-special 31 | ulab-utils 32 | ulab-tricks 33 | ulab-programming 34 | 35 | Indices and tables 36 | ================== 37 | 38 | * :ref:`genindex` 39 | * :ref:`modindex` 40 | * :ref:`search` 41 | -------------------------------------------------------------------------------- /docs/manual/source/scipy-signal.rst: -------------------------------------------------------------------------------- 1 | 2 | scipy.signal 3 | ============ 4 | 5 | This module defines the single function: 6 | 7 | 1. `scipy.signal.sosfilt <#sosfilt>`__ 8 | 9 | sosfilt 10 | ------- 11 | 12 | ``scipy``: 13 | https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.sosfilt.html 14 | 15 | Filter data along one dimension using cascaded second-order sections. 16 | 17 | The function takes two positional arguments, ``sos``, the filter 18 | segments of length 6, and the one-dimensional, uniformly sampled data 19 | set to be filtered. Returns the filtered data, or the filtered data and 20 | the final filter delays, if the ``zi`` keyword arguments is supplied. 21 | The keyword argument must be a float ``ndarray`` of shape 22 | ``(n_sections, 2)``. If ``zi`` is not passed to the function, the 23 | initial values are assumed to be 0. 24 | 25 | .. code:: 26 | 27 | # code to be run in micropython 28 | 29 | from ulab import numpy as np 30 | from ulab import scipy as spy 31 | 32 | x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 33 | sos = [[1, 2, 3, 1, 5, 6], [1, 2, 3, 1, 5, 6]] 34 | y = spy.signal.sosfilt(sos, x) 35 | print('y: ', y) 36 | 37 | .. parsed-literal:: 38 | 39 | y: array([0.0, 1.0, -4.0, 24.0, -104.0, 440.0, -1728.0, 6532.000000000001, -23848.0, 84864.0], dtype=float) 40 | 41 | 42 | 43 | 44 | .. code:: 45 | 46 | # code to be run in micropython 47 | 48 | from ulab import numpy as np 49 | from ulab import scipy as spy 50 | 51 | x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 52 | sos = [[1, 2, 3, 1, 5, 6], [1, 2, 3, 1, 5, 6]] 53 | # initial conditions of the filter 54 | zi = np.array([[1, 2], [3, 4]]) 55 | 56 | y, zf = spy.signal.sosfilt(sos, x, zi=zi) 57 | print('y: ', y) 58 | print('\n' + '='*40 + '\nzf: ', zf) 59 | 60 | .. parsed-literal:: 61 | 62 | y: array([4.0, -16.0, 63.00000000000001, -227.0, 802.9999999999999, -2751.0, 9271.000000000001, -30775.0, 101067.0, -328991.0000000001], dtype=float) 63 | 64 | ======================================== 65 | zf: array([[37242.0, 74835.0], 66 | [1026187.0, 1936542.0]], dtype=float) 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /docs/manual/source/scipy-special.rst: -------------------------------------------------------------------------------- 1 | 2 | scipy.special 3 | ============= 4 | 5 | ``scipy``\ ’s ``special`` module defines several functions that behave 6 | as do the standard mathematical functions of the ``numpy``, i.e., they 7 | can be called on any scalar, scalar-valued iterable (ranges, lists, 8 | tuples containing numbers), and on ``ndarray``\ s without having to 9 | change the call signature. In all cases the functions return a new 10 | ``ndarray`` of typecode ``float`` (since these functions usually 11 | generate float values, anyway). 12 | 13 | At present, ``ulab``\ ’s ``special`` module contains the following 14 | functions: 15 | 16 | ``erf``, ``erfc``, ``gamma``, and ``gammaln``, and they can be called by 17 | prepending them by ``scipy.special.``. 18 | 19 | .. code:: 20 | 21 | # code to be run in micropython 22 | 23 | from ulab import numpy as np 24 | from ulab import scipy as spy 25 | 26 | a = range(9) 27 | b = np.array(a) 28 | 29 | print('a: ', a) 30 | print(spy.special.erf(a)) 31 | 32 | print('\nb: ', b) 33 | print(spy.special.erfc(b)) 34 | 35 | .. parsed-literal:: 36 | 37 | a: range(0, 9) 38 | array([0.0, 0.8427007929497149, 0.9953222650189527, 0.9999779095030014, 0.9999999845827421, 1.0, 1.0, 1.0, 1.0], dtype=float64) 39 | 40 | b: array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0], dtype=float64) 41 | array([1.0, 0.1572992070502851, 0.004677734981047265, 2.209049699858544e-05, 1.541725790028002e-08, 1.537459794428035e-12, 2.151973671249892e-17, 4.183825607779414e-23, 1.122429717298293e-29], dtype=float64) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/templates/manual.tpl: -------------------------------------------------------------------------------- 1 | 2 | {%- extends 'display_priority.tpl' -%} 3 | 4 | 5 | {% block in_prompt %} 6 | {% endblock in_prompt %} 7 | 8 | {% block output_prompt %} 9 | {% endblock output_prompt %} 10 | 11 | {% block input scoped%} 12 | 13 | {%- if cell.source.split('\n')[0].startswith('%%micropython') -%} 14 | .. code:: 15 | 16 | {{ '\n'.join(['# code to be run in micropython'] + cell.source.strip().split('\n')[1:]) | indent}} 17 | 18 | {%- else -%} 19 | .. code:: 20 | 21 | {{ '\n'.join(['# code to be run in CPython\n'] + cell.source.strip().split('\n')) | indent}} 22 | {%- endif -%} 23 | {% endblock input %} 24 | 25 | {% block error %} 26 | :: 27 | 28 | {{ super() }} 29 | {% endblock error %} 30 | 31 | {% block traceback_line %} 32 | {{ line | indent | strip_ansi }} 33 | {% endblock traceback_line %} 34 | 35 | {% block execute_result %} 36 | {% block data_priority scoped %} 37 | {{ super() }} 38 | {% endblock %} 39 | {% endblock execute_result %} 40 | 41 | {% block stream %} 42 | .. parsed-literal:: 43 | 44 | {{ output.text | indent }} 45 | {% endblock stream %} 46 | 47 | {% block data_svg %} 48 | .. image:: {{ output.metadata.filenames['image/svg+xml'] | urlencode }} 49 | {% endblock data_svg %} 50 | 51 | {% block data_png %} 52 | .. image:: {{ output.metadata.filenames['image/png'] | urlencode }} 53 | {%- set width=output | get_metadata('width', 'image/png') -%} 54 | {%- if width is not none %} 55 | :width: {{ width }}px 56 | {%- endif %} 57 | {%- set height=output | get_metadata('height', 'image/png') -%} 58 | {%- if height is not none %} 59 | :height: {{ height }}px 60 | {%- endif %} 61 | {% endblock data_png %} 62 | 63 | {% block data_jpg %} 64 | .. image:: {{ output.metadata.filenames['image/jpeg'] | urlencode }} 65 | {%- set width=output | get_metadata('width', 'image/jpeg') -%} 66 | {%- if width is not none %} 67 | :width: {{ width }}px 68 | {%- endif %} 69 | {%- set height=output | get_metadata('height', 'image/jpeg') -%} 70 | {%- if height is not none %} 71 | :height: {{ height }}px 72 | {%- endif %} 73 | {% endblock data_jpg %} 74 | 75 | {% block data_markdown %} 76 | {{ output.data['text/markdown'] | convert_pandoc("markdown", "rst") }} 77 | {% endblock data_markdown %} 78 | 79 | {% block data_latex %} 80 | .. math:: 81 | 82 | {{ output.data['text/latex'] | strip_dollars | indent }} 83 | {% endblock data_latex %} 84 | 85 | {% block data_text scoped %} 86 | .. parsed-literal:: 87 | 88 | {{ output.data['text/plain'] | indent }} 89 | {% endblock data_text %} 90 | 91 | {% block data_html scoped %} 92 | .. raw:: html 93 | 94 | {{ output.data['text/html'] | indent }} 95 | {% endblock data_html %} 96 | 97 | {% block markdowncell scoped %} 98 | {{ cell.source | convert_pandoc("markdown", "rst") }} 99 | {% endblock markdowncell %} 100 | 101 | {%- block rawcell scoped -%} 102 | {%- if cell.metadata.get('raw_mimetype', '').lower() in resources.get('raw_mimetypes', ['']) %} 103 | {{cell.source}} 104 | {% endif -%} 105 | {%- endblock rawcell -%} 106 | 107 | {% block headingcell scoped %} 108 | {{ ("#" * cell.level + cell.source) | replace('\n', ' ') | convert_pandoc("markdown", "rst") }} 109 | {% endblock headingcell %} 110 | 111 | {% block unknowncell scoped %} 112 | unknown type {{cell.type}} 113 | {% endblock unknowncell %} 114 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | sphinx-autoapi 2 | -------------------------------------------------------------------------------- /requirements_cp_dev.txt: -------------------------------------------------------------------------------- 1 | # For docs 2 | mypy 3 | black 4 | isort 5 | astroid 6 | setuptools 7 | setuptools_scm 8 | 9 | Sphinx>=4.0.0 10 | sphinx-autoapi 11 | sphinx-rtd-theme 12 | sphinxcontrib-svg2pdfconverter 13 | readthedocs-sphinx-search 14 | myst-parser 15 | 16 | # For stubs and annotations 17 | adafruit-circuitpython-typing 18 | build 19 | -------------------------------------------------------------------------------- /snippets/json_to_ndarray.py: -------------------------------------------------------------------------------- 1 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 2 | # 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2022 Zoltán Vörös 6 | 7 | import sys 8 | 9 | use_ulab = False 10 | 11 | try: 12 | from ubinascii import a2b_base64 as b64decode 13 | from ubinascii import unhexlify 14 | import ujson as json 15 | from ulab import numpy as np 16 | use_ulab = True 17 | except: 18 | from base64 import b64decode 19 | import json 20 | import numpy as np 21 | from numpy.lib.format import descr_to_dtype 22 | 23 | def ulab_descr_to_dtype(descriptor): 24 | descriptor = descriptor[1:] 25 | 26 | if descriptor == 'u1': 27 | return np.uint8 28 | elif descriptor == 'i1': 29 | return np.int8 30 | if descriptor == 'u2': 31 | return np.uint16 32 | if descriptor == 'i2': 33 | return np.int16 34 | elif descriptor == 'f8': 35 | if np.float != ord('d'): 36 | raise TypeError('doubles are not supported') 37 | else: 38 | return np.float 39 | elif descriptor == 'f16': 40 | if np.float != ord('f'): 41 | raise TypeError('') 42 | else: 43 | return np.float 44 | else: 45 | raise TypeError('descriptor could not be decoded') 46 | 47 | 48 | def json_to_ndarray(json_string, b64=True): 49 | """ 50 | Turn a json string into an ndarray 51 | The string must be the representation of a dictionary with the three keys 52 | 53 | - dtype: a valid numpy dtype string (one of |u1, |i1, u2, >i2, >f4, >f8, >c8, >c16) 54 | - array: the hexified, or base64-encoded raw data array 55 | - shape: the shape of the array (a list or tuple of integers) 56 | 57 | Usage: 58 | str = '{"dtype": "u2, >i2, >f4, >f8, >c8, >c16) 53 | - array: the hexified, or base64-encoded raw data array 54 | - shape: the shape of the array (a list or tuple of integers) 55 | 56 | Usage: 57 | ndarray = np.array([1, 2, 3], dtype=np.uint8) 58 | ndarray_to_json(ndarray, b64=True) 59 | """ 60 | 61 | if not isinstance(obj, np.ndarray): 62 | raise TypeError('input argument must be an ndarray') 63 | 64 | if use_ulab: 65 | dtype_desciptor = ulab_dtype_to_descr(obj.dtype) 66 | else: 67 | dtype_desciptor = dtype_to_descr(obj.dtype) 68 | 69 | if not b64: 70 | data = hexlify(obj.tobytes()) 71 | else: 72 | data = b64encode(obj.tobytes()) 73 | 74 | return json.dumps({'array': data, 'dtype': dtype_desciptor, 'shape': obj.shape}) 75 | -------------------------------------------------------------------------------- /snippets/numpy/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | from . import core 3 | from .core import * 4 | from . import lib 5 | from .lib import * -------------------------------------------------------------------------------- /snippets/numpy/core/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | from .multiarray import * 3 | from .numeric import * 4 | from .fromnumeric import * 5 | from .shape_base import * -------------------------------------------------------------------------------- /snippets/numpy/core/fromnumeric.py: -------------------------------------------------------------------------------- 1 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 2 | # 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2022 Phil Jepsen 6 | 7 | from .overrides import set_module 8 | from .multiarray import asarray 9 | from ulab import numpy as np 10 | from ... import numpy 11 | 12 | def prod(arr): 13 | result = 1 14 | for x in arr: 15 | result = result * x 16 | return result 17 | 18 | def size(a, axis=None): 19 | """ 20 | Return the number of elements along a given axis. 21 | Parameters 22 | ---------- 23 | a : array_like 24 | Input data. 25 | axis : int, optional 26 | Axis along which the elements are counted. By default, give 27 | the total number of elements. 28 | Returns 29 | ------- 30 | element_count : int 31 | Number of elements along the specified axis. 32 | See Also 33 | -------- 34 | shape : dimensions of array 35 | ndarray.shape : dimensions of array 36 | ndarray.size : number of elements in array 37 | Examples 38 | -------- 39 | >>> a = np.array([[1,2,3],[4,5,6]]) 40 | >>> np.size(a) 41 | 6 42 | >>> np.size(a,1) 43 | 3 44 | >>> np.size(a,0) 45 | 2 46 | """ 47 | if axis is None: 48 | try: 49 | return a.size 50 | except AttributeError: 51 | return asarray(a).size 52 | else: 53 | try: 54 | return a.shape[axis] 55 | except AttributeError: 56 | return asarray(a).shape[axis] 57 | 58 | def nonzero(a): 59 | if not isinstance(a,(np.ndarray)): 60 | a = asarray(a) 61 | x = a.shape 62 | row = x[0] 63 | if len(x) == 1: 64 | column = 0 65 | else: 66 | column = x[1] 67 | 68 | nonzero_row = np.array([],dtype=np.float) 69 | nonzero_col = np.array([],dtype=np.float) 70 | 71 | if column == 0: 72 | for i in range(0,row): 73 | if a[i] != 0: 74 | nonzero_row = numpy.append(nonzero_row,i) 75 | return (np.array(nonzero_row, dtype=np.int8),) 76 | 77 | for i in range(0,row): 78 | for j in range(0,column): 79 | if a[i,j] != 0: 80 | nonzero_row = numpy.append(nonzero_row,i) 81 | nonzero_col = numpy.append(nonzero_col,j) 82 | 83 | return (np.array(nonzero_row, dtype=np.int8), np.array(nonzero_col, dtype=np.int8)) -------------------------------------------------------------------------------- /snippets/numpy/core/multiarray.py: -------------------------------------------------------------------------------- 1 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 2 | # 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2022 Phil Jepsen 6 | 7 | from ulab import numpy as np 8 | 9 | def asarray(a, dtype=None): 10 | if isinstance(a,(np.ndarray)): 11 | return a 12 | try: 13 | if dtype is not None: 14 | a = np.array([a], dtype=dtype) 15 | elif isinstance(a, list) or isinstance(a, tuple): 16 | a = np.array(a) 17 | else: 18 | a = np.array([a]) 19 | return a 20 | except Exception as e: 21 | if "can't convert complex to float" in e.args or "'complex' object isn't iterable" in e.args: 22 | try: 23 | a = np.array([a], dtype=np.complex).flatten() 24 | return a 25 | except: 26 | pass 27 | raise ValueError('Could not cast %s to array' % (a)) 28 | -------------------------------------------------------------------------------- /snippets/numpy/core/numeric.py: -------------------------------------------------------------------------------- 1 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 2 | # 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2022 Phil Jepsen 6 | 7 | from ulab import numpy as np 8 | from .multiarray import (asarray) 9 | 10 | def zeros_like(a, dtype=None, order='K', subok=True, shape=None): 11 | """ 12 | Return an array of zeros with the same shape and type as a given array. 13 | Parameters 14 | ---------- 15 | a : array_like 16 | The shape and data-type of `a` define these same attributes of 17 | the returned array. 18 | dtype : data-type, optional 19 | Overrides the data type of the result. 20 | .. versionadded:: 1.6.0 21 | order : {'C', 'F', 'A', or 'K'}, optional 22 | Overrides the memory layout of the result. 'C' means C-order, 23 | 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous, 24 | 'C' otherwise. 'K' means match the layout of `a` as closely 25 | as possible. 26 | .. versionadded:: 1.6.0 27 | subok : bool, optional. 28 | If True, then the newly created array will use the sub-class 29 | type of `a`, otherwise it will be a base-class array. Defaults 30 | to True. 31 | shape : int or sequence of ints, optional. 32 | Overrides the shape of the result. If order='K' and the number of 33 | dimensions is unchanged, will try to keep order, otherwise, 34 | order='C' is implied. 35 | .. versionadded:: 1.17.0 36 | Returns 37 | ------- 38 | out : ndarray 39 | Array of zeros with the same shape and type as `a`. 40 | See Also 41 | -------- 42 | empty_like : Return an empty array with shape and type of input. 43 | ones_like : Return an array of ones with shape and type of input. 44 | full_like : Return a new array with shape of input filled with value. 45 | zeros : Return a new array setting values to zero. 46 | Examples 47 | -------- 48 | >>> x = np.arange(6) 49 | >>> x = x.reshape((2, 3)) 50 | >>> x 51 | array([[0, 1, 2], 52 | [3, 4, 5]]) 53 | >>> np.zeros_like(x) 54 | array([[0, 0, 0], 55 | [0, 0, 0]]) 56 | >>> y = np.arange(3, dtype=float) 57 | >>> y 58 | array([0., 1., 2.]) 59 | >>> np.zeros_like(y) 60 | array([0., 0., 0.]) 61 | """ 62 | 63 | res = np.full(a.shape, 0, dtype=a.dtype) 64 | return res 65 | 66 | -------------------------------------------------------------------------------- /snippets/numpy/core/overrides.py: -------------------------------------------------------------------------------- 1 | 2 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 3 | # 4 | # The MIT License (MIT) 5 | # 6 | # Copyright (c) 2022 Phil Jepsen 7 | 8 | import sys 9 | 10 | def set_module(module): 11 | """Decorator for overriding __module__ on a function or class. 12 | Example usage:: 13 | @set_module('numpy') 14 | def example(): 15 | pass 16 | assert example.__module__ == 'numpy' 17 | """ 18 | def decorator(func): 19 | if module is not None: 20 | sys.modules[func.__globals__['__name__']] = module 21 | return func 22 | return decorator -------------------------------------------------------------------------------- /snippets/numpy/core/shape_base.py: -------------------------------------------------------------------------------- 1 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 2 | # 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2022 Phil Jepsen 6 | 7 | from ulab import numpy as np 8 | from .multiarray import asarray 9 | 10 | def atleast_1d(*arys): 11 | res = [] 12 | for ary in arys: 13 | ary = asarray(ary) 14 | if not isinstance(ary,(np.ndarray)): 15 | result = ary.reshape((1,)) 16 | else: 17 | result = ary 18 | res.append(result) 19 | if len(res) == 1: 20 | return res[0] 21 | else: 22 | return res -------------------------------------------------------------------------------- /snippets/numpy/lib/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | from .function_base import * 3 | from .polynomial import * 4 | from .type_check import * 5 | from .block import * -------------------------------------------------------------------------------- /snippets/numpy/lib/block.py: -------------------------------------------------------------------------------- 1 | from ulab.numpy import zeros 2 | 3 | def block(S): 4 | w = sum([len(m[0]) for m in S[0]]) 5 | h = sum([len(row[0]) for row in S]) 6 | M = zeros((h, w)) 7 | i = 0 8 | j = 0 9 | for row in S: 10 | di = len(row[0]) 11 | for matrix in row: 12 | dj = len(matrix[0]) 13 | M[i:i + di, j:j + dj] = matrix 14 | j += dj 15 | i += di 16 | j = 0 17 | return M -------------------------------------------------------------------------------- /snippets/numpy/lib/function_base.py: -------------------------------------------------------------------------------- 1 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 2 | # 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2022 Phil Jepsen 6 | 7 | from ulab import numpy as np 8 | from ..core.multiarray import (asarray) 9 | from ..core.overrides import set_module 10 | 11 | @set_module('numpy') 12 | def append(arr, values, axis=None): 13 | arr = asarray(arr) 14 | values = asarray(values) 15 | if axis is None: 16 | if len(arr.shape) != 1: 17 | arr = arr.flatten() 18 | values = values.flatten() 19 | axis = len(arr.shape)-1 20 | return np.concatenate((arr, values), axis=axis) 21 | -------------------------------------------------------------------------------- /snippets/numpy/lib/polynomial.py: -------------------------------------------------------------------------------- 1 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 2 | # 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2022 Phil Jepsen 6 | 7 | from ..core import (atleast_1d, asarray) 8 | from ..core.overrides import set_module 9 | from ulab import numpy as np 10 | 11 | @set_module('numpy') 12 | def poly(seq_of_zeros): 13 | seq_of_zeros = atleast_1d(seq_of_zeros) 14 | sh = seq_of_zeros.shape 15 | 16 | if len(sh) == 2 and sh[0] == sh[1] and sh[0] != 0: 17 | seq_of_zeros = np.linalg.eig(seq_of_zeros) 18 | elif len(sh) == 1: 19 | dt = seq_of_zeros.dtype 20 | # Let object arrays slip through, e.g. for arbitrary precision 21 | if dt != object: 22 | seq_of_zeros = seq_of_zeros #seq_of_zeros.astype(mintypecode(dt.char)) 23 | else: 24 | raise ValueError("input must be 1d or non-empty square 2d array.") 25 | 26 | if len(seq_of_zeros) == 0: 27 | return 1.0 28 | dt = seq_of_zeros.dtype 29 | a = np.ones((1,), dtype=dt) 30 | 31 | for k in range(len(seq_of_zeros)): 32 | a = np.convolve(a, np.array([1, -seq_of_zeros[k]], dtype=dt)) 33 | 34 | if a.dtype == np.complex: 35 | # if complex roots are all complex conjugates, the roots are real. 36 | roots = asarray(seq_of_zeros, complex) 37 | p = np.sort_complex(roots) 38 | c = np.real(p) - np.imag(p) * 1j 39 | q = np.sort_complex(c) 40 | if np.all(p == q): 41 | a = a.real.copy() 42 | return a -------------------------------------------------------------------------------- /snippets/numpy/lib/type_check.py: -------------------------------------------------------------------------------- 1 | 2 | from ulab import numpy as np 3 | from ..core.multiarray import (asarray) 4 | from ..core.overrides import set_module 5 | 6 | @set_module('numpy') 7 | 8 | # This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab 9 | # 10 | # The MIT License (MIT) 11 | # 12 | # Copyright (c) 2022 Phil Jepsen 13 | 14 | def _isreal(a): 15 | result = [] 16 | for x in a: 17 | if isinstance(x, float): 18 | result.append(True) 19 | elif isinstance(x, complex) and x.imag == 0: 20 | result.append(True) 21 | else: 22 | result.append(False) 23 | return result 24 | 25 | def isreal(x): 26 | """ 27 | Returns a bool array, where True if input element is real. 28 | If element has complex type with zero complex part, the return value 29 | for that element is True. 30 | Parameters 31 | ---------- 32 | x : array_like 33 | Input array. 34 | Returns 35 | ------- 36 | out : ndarray, bool 37 | Boolean array of same shape as `x`. 38 | Notes 39 | ----- 40 | `isreal` may behave unexpectedly for string or object arrays (see examples) 41 | See Also 42 | -------- 43 | iscomplex 44 | isrealobj : Return True if x is not a complex type. 45 | Examples 46 | -------- 47 | >>> a = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j], dtype=complex) 48 | >>> np.isreal(a) 49 | array([False, True, True, True, True, False]) 50 | 51 | The function does not work on string arrays. 52 | >>> a = np.array([2j, "a"], dtype="U") 53 | >>> np.isreal(a) # Warns about non-elementwise comparison 54 | False 55 | 56 | Returns True for all elements in input array of ``dtype=object`` even if 57 | any of the elements is complex. 58 | >>> a = np.array([1, "2", 3+4j], dtype=object) 59 | >>> np.isreal(a) 60 | array([ True, True, True]) 61 | 62 | isreal should not be used with object arrays 63 | 64 | >>> a = np.array([1+2j, 2+1j], dtype=object) 65 | >>> np.isreal(a) 66 | array([ True, True]) 67 | """ 68 | x = asarray(x) 69 | result = _isreal(x) 70 | return result if len(result) > 1 else result[0] 71 | -------------------------------------------------------------------------------- /snippets/rclass.py: -------------------------------------------------------------------------------- 1 | from typing import List, Tuple, Union # upip.install("pycopy-typing") 2 | from ulab import numpy as np 3 | 4 | _DType = int 5 | _RClassKeyType = Union[slice, int, float, list, tuple, np.ndarray] 6 | 7 | # this is a stripped down version of RClass (used by np.r_[...etc]) 8 | # it doesn't include support for string arguments as the first index element 9 | class RClass: 10 | 11 | def __getitem__(self, key: Union[_RClassKeyType, Tuple[_RClassKeyType, ...]]): 12 | 13 | if not isinstance(key, tuple): 14 | key = (key,) 15 | 16 | objs: List[np.ndarray] = [] 17 | scalars: List[int] = [] 18 | arraytypes: List[_DType] = [] 19 | scalartypes: List[_DType] = [] 20 | 21 | # these may get overridden in following loop 22 | axis = 0 23 | 24 | for idx, item in enumerate(key): 25 | scalar = False 26 | 27 | try: 28 | if isinstance(item, np.ndarray): 29 | newobj = item 30 | 31 | elif isinstance(item, slice): 32 | step = item.step 33 | start = item.start 34 | stop = item.stop 35 | if start is None: 36 | start = 0 37 | if step is None: 38 | step = 1 39 | if isinstance(step, complex): 40 | size = int(abs(step)) 41 | newobj: np.ndarray = np.linspace(start, stop, num=size) 42 | else: 43 | newobj = np.arange(start, stop, step) 44 | 45 | # if is number 46 | elif isinstance(item, (int, float, bool)): 47 | newobj = np.array([item]) 48 | scalars.append(len(objs)) 49 | scalar = True 50 | scalartypes.append(newobj.dtype()) 51 | 52 | else: 53 | newobj = np.array(item) 54 | 55 | except TypeError: 56 | raise Exception("index object %s of type %s is not supported by r_[]" % ( 57 | str(item), type(item))) 58 | 59 | objs.append(newobj) 60 | if not scalar and isinstance(newobj, np.ndarray): 61 | arraytypes.append(newobj.dtype()) 62 | 63 | # Ensure that scalars won't up-cast unless warranted 64 | final_dtype = min(arraytypes + scalartypes) 65 | for idx, obj in enumerate(objs): 66 | if obj.dtype != final_dtype: 67 | objs[idx] = np.array(objs[idx], dtype=final_dtype) 68 | 69 | return np.concatenate(tuple(objs), axis=axis) 70 | 71 | # this seems weird - not sure what it's for 72 | def __len__(self): 73 | return 0 74 | 75 | r_ = RClass() 76 | -------------------------------------------------------------------------------- /snippets/scipy/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | from . import signal 3 | from .signal import * -------------------------------------------------------------------------------- /snippets/scipy/signal/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | from .filter_design import * -------------------------------------------------------------------------------- /snippets/tests/numpy/core/fromnumeric.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | sys.path.append('.') 4 | 5 | from snippets import numpy 6 | from ulab import numpy as np 7 | 8 | a = np.array([[1,2,3],[4,5,6]]) 9 | print(numpy.size(a)) 10 | print(numpy.size(a,1)) 11 | print(numpy.size(a,0)) 12 | 13 | print(numpy.prod([1, 10, 100, 5])) 14 | print(numpy.prod([])) 15 | print(numpy.prod([1.,2.])) 16 | 17 | 18 | a = np.array([1,2,3]) 19 | print(numpy.nonzero(a)) 20 | b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 21 | print(numpy.nonzero(b > 3)) 22 | 23 | c = np.array([0,1,0,-1]) 24 | print(numpy.nonzero(c > 0)) 25 | print(numpy.nonzero(c < 0)) 26 | 27 | 28 | -------------------------------------------------------------------------------- /snippets/tests/numpy/core/fromnumeric.py.exp: -------------------------------------------------------------------------------- 1 | 6 2 | 3 3 | 2 4 | 5000 5 | 1 6 | 2.0 7 | (array([0, 1, 2], dtype=int8),) 8 | (array([1, 1, 1, 2, 2, 2], dtype=int8), array([0, 1, 2, 0, 1, 2], dtype=int8)) 9 | (array([1], dtype=int8),) 10 | (array([3], dtype=int8),) 11 | -------------------------------------------------------------------------------- /snippets/tests/numpy/core/multiarray.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | sys.path.append('.') 4 | 5 | from snippets import numpy 6 | from ulab import numpy as np 7 | np.set_printoptions(threshold=100) 8 | 9 | print (numpy.asarray([1])) 10 | print (numpy.asarray([1.0, 2.0, 3j])) 11 | print (numpy.asarray([4, 3, 1, (2-2j), (2+2j), (2-1j), (2+1j), (2-1j), (2+1j), (1+1j), (1-1j)])) -------------------------------------------------------------------------------- /snippets/tests/numpy/core/multiarray.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0], dtype=float64) 2 | array([1.0+0.0j, 2.0+0.0j, 0.0+3.0j], dtype=complex) 3 | array([4.0+0.0j, 3.0+0.0j, 1.0+0.0j, 2.0-2.0j, 2.0+2.0j, 2.0-1.0j, 2.0+1.0j, 2.0-1.0j, 2.0+1.0j, 1.0+1.0j, 1.0-1.0j], dtype=complex) 4 | -------------------------------------------------------------------------------- /snippets/tests/numpy/core/numeric.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | sys.path.append('.') 4 | 5 | from ulab import numpy as np 6 | from snippets import numpy 7 | 8 | x = np.array([[0, 1, 2], 9 | [3, 4, 5]]) 10 | print(numpy.zeros_like(x)) 11 | 12 | y = np.array([[0, 1j, -2j],[3, 4, 5]], dtype=np.complex) 13 | print(numpy.zeros_like(y)) 14 | 15 | -------------------------------------------------------------------------------- /snippets/tests/numpy/core/numeric.py.exp: -------------------------------------------------------------------------------- 1 | array([[0.0, 0.0, 0.0], 2 | [0.0, 0.0, 0.0]], dtype=float64) 3 | array([[0.0+0.0j, 0.0+0.0j, 0.0+0.0j], 4 | [0.0+0.0j, 0.0+0.0j, 0.0+0.0j]], dtype=complex) 5 | -------------------------------------------------------------------------------- /snippets/tests/numpy/core/shape_base.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | sys.path.append('.') 4 | 5 | from ulab import numpy as np 6 | from snippets import numpy 7 | 8 | print(numpy.atleast_1d(1.0)) 9 | 10 | x = np.arange(9.0).reshape((3,3)) 11 | 12 | print(numpy.atleast_1d(x)) 13 | print(numpy.atleast_1d(x) is x) 14 | 15 | print(numpy.atleast_1d(1, [3, 4])) 16 | -------------------------------------------------------------------------------- /snippets/tests/numpy/core/shape_base.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0], dtype=float64) 2 | array([[0.0, 1.0, 2.0], 3 | [3.0, 4.0, 5.0], 4 | [6.0, 7.0, 8.0]], dtype=float64) 5 | True 6 | [array([1.0], dtype=float64), array([3.0, 4.0], dtype=float64)] 7 | -------------------------------------------------------------------------------- /snippets/tests/numpy/lib/block.py: -------------------------------------------------------------------------------- 1 | from ulab.numpy import array, zeros, eye, ones 2 | from snippets import numpy 3 | 4 | a = array([[1, 1]]) 5 | b = array([[2]]) 6 | c = array( 7 | [[3, 3], 8 | [3, 3], 9 | [3, 3]]) 10 | d = array( 11 | [[4], 12 | [4], 13 | [4]]) 14 | print(numpy.block([[a, b], [c, d]])) 15 | a = zeros((2, 3)) 16 | b = eye(2) * 2 17 | c = eye(3) * 5 18 | d = ones((3, 2)) 19 | print(numpy.block([[a, b], [c, d]])) 20 | -------------------------------------------------------------------------------- /snippets/tests/numpy/lib/block.py.exp: -------------------------------------------------------------------------------- 1 | array([[1, 1, 2], 2 | [3, 3, 4], 3 | [3, 3, 4], 4 | [3, 3, 4]]) 5 | array([[0, 0, 0, 2, 0], 6 | [0, 0, 0, 0, 2], 7 | [5, 0, 0, 1, 1], 8 | [0, 5, 0, 1, 1], 9 | [0, 0, 5, 1, 1]]) -------------------------------------------------------------------------------- /snippets/tests/numpy/lib/function_base.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | sys.path.append('.') 4 | 5 | from snippets import numpy 6 | from ulab import numpy as np 7 | 8 | print(numpy.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])) 9 | 10 | print(numpy.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)) 11 | 12 | -------------------------------------------------------------------------------- /snippets/tests/numpy/lib/function_base.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64) 2 | array([[1.0, 2.0, 3.0], 3 | [4.0, 5.0, 6.0], 4 | [7.0, 8.0, 9.0]], dtype=float64) 5 | -------------------------------------------------------------------------------- /snippets/tests/numpy/lib/polynomial.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | sys.path.append('.') 4 | 5 | from snippets import numpy 6 | from ulab import numpy as np 7 | 8 | 9 | print(numpy.poly((0, 0, 0))) 10 | 11 | print(numpy.poly((-1./2, 0, 1./2))) 12 | 13 | print(numpy.poly((0.847, 0, 0.9883))) 14 | 15 | #P = np.array([[0, 1./3], [-1./2, 0]]) 16 | #print(numpy.poly(P)) 17 | -------------------------------------------------------------------------------- /snippets/tests/numpy/lib/polynomial.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0, 0.0, 0.0, 0.0], dtype=float64) 2 | array([1.0, 0.0, -0.25, 0.0], dtype=float64) 3 | array([1.0, -1.8353, 0.8370901, 0.0], dtype=float64) 4 | -------------------------------------------------------------------------------- /snippets/tests/numpy/lib/type_check.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | sys.path.append('.') 4 | 5 | from snippets import numpy 6 | from ulab import numpy as np 7 | 8 | a = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j], dtype=np.complex) 9 | print(numpy.isreal(a)) 10 | 11 | 12 | a = np.array([1+2j, 2+1j], dtype=np.complex) 13 | print(numpy.isreal(a)) 14 | 15 | 16 | print(numpy.isreal(1)) 17 | print(numpy.isreal(1j)) -------------------------------------------------------------------------------- /snippets/tests/numpy/lib/type_check.py.exp: -------------------------------------------------------------------------------- 1 | [False, True, True, True, True, False] 2 | [False, False] 3 | True 4 | False 5 | -------------------------------------------------------------------------------- /snippets/tests/scipy/signal/filter_design.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | sys.path.append('.') 4 | 5 | from snippets import scipy 6 | from ulab import numpy as np 7 | 8 | np.set_printoptions(threshold=100) 9 | 10 | a = [4, 3, 1, 2-2j, 2+2j, 2-1j, 2+1j, 2-1j, 2+1j, 1+1j, 1-1j] 11 | #print('_cplxreal: ', scipy.cplxreal(a)) 12 | f = np.array([-1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0], dtype=np.float) 13 | t = (0.9984772174419884+0.01125340518638924j) 14 | w = 'real' 15 | #print('nearest_real_complex_idx: ', scipy.nearest_real_complex_idx(f,t,w)) 16 | 17 | 18 | nyquistRate = 48000 * 2 19 | centerFrequency_Hz = 480.0 20 | lowerCutoffFrequency_Hz = centerFrequency_Hz/math.sqrt(2) 21 | upperCutoffFrequenc_Hz = centerFrequency_Hz*math.sqrt(2) 22 | wn = np.array([ lowerCutoffFrequency_Hz, upperCutoffFrequenc_Hz])/nyquistRate 23 | 24 | z = [] 25 | p = np.array([-0.1564344650402309+0.9876883405951379j, -0.4539904997395468+0.8910065241883679j, 26 | -0.7071067811865476+0.7071067811865475j, -0.8910065241883679+0.4539904997395467j, -0.9876883405951379+0.1564344650402309j, 27 | -0.9876883405951379-0.1564344650402309j, -0.8910065241883679-0.4539904997395467j, -0.7071067811865476-0.7071067811865475j, 28 | -0.4539904997395468-0.8910065241883679j, -0.1564344650402309-0.9876883405951379j], dtype=np.complex) 29 | k = 1 30 | wo = 0.1886352115099219 31 | 32 | print(scipy.lp2hp_zpk(z,p,k,wo)) 33 | 34 | z = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float) 35 | p = np.array([-0.02950904840030544-0.1863127990340476j, -0.08563859394186457-0.1680752041469931j, 36 | -0.1333852372292245-0.1333852372292244j, -0.1680752041469931-0.08563859394186453j, -0.1863127990340476-0.02950904840030543j, 37 | -0.1863127990340476+0.02950904840030543j, -0.1680752041469931+0.08563859394186453j, -0.1333852372292245+0.1333852372292244j, 38 | -0.08563859394186457+0.1680752041469931j, -0.02950904840030544+0.1863127990340476j], dtype=np.complex) 39 | k = 1.0 40 | fs = 2.0 41 | 42 | print(scipy.bilinear_zpk(z,p,k,fs)) 43 | 44 | z = np.array([], dtype=np.float) 45 | p = np.array([-0.3826834323650898+0.9238795325112868j, 46 | -0.9238795325112868+0.3826834323650898j, -0.9238795325112868-0.3826834323650898j, 47 | -0.3826834323650898-0.9238795325112868j], dtype=np.complex) 48 | k = 1 49 | wo = 0.03141673402115484 50 | bw = 0.02221601345771878 51 | print(scipy.lp2bs_zpk(z, p, k, wo=wo, bw=bw)) 52 | 53 | print(scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='ba')) 54 | print(scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='zpk')) 55 | print(scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='sos')) 56 | print(scipy.butter(N=4, Wn=wn, btype='bandstop', analog=False, output='ba')) 57 | print(scipy.butter(10, 15, 'lp', fs=1000, output='sos')) 58 | print(scipy.butter(10, 15, 'hp', fs=1000, output='sos')) 59 | 60 | -------------------------------------------------------------------------------- /test-common.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | dims="$1" 4 | micropython="$2" 5 | for level1 in $(printf "%dd " $(seq 1 ${dims})) 6 | do 7 | for level2 in numpy scipy utils complex; do 8 | rm -f *.exp 9 | if [ ! -d tests/"$level1"/"$level2" ]; then 10 | break; 11 | fi 12 | for file in tests/"$level1"/"$level2"/*.py; do 13 | if [ ! -f "$file"".exp" ]; then 14 | echo "" > "$file"".exp" 15 | fi 16 | done 17 | if ! env MICROPY_MICROPYTHON="$micropython" ./run-tests -d tests/"$level1"/"$level2"; then 18 | for exp in *.exp; do 19 | testbase=$(basename $exp .exp); 20 | echo "\nFAILURE $testbase"; 21 | diff -u $testbase.exp $testbase.out; 22 | done 23 | exit 1 24 | fi 25 | done 26 | done 27 | 28 | -------------------------------------------------------------------------------- /test-snippets.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | micropython="$1" 4 | for level1 in numpy scipy; 5 | do 6 | for level2 in core lib signal; do 7 | rm -f *.exp 8 | if ! env MICROPY_MICROPYTHON="$micropython" ./run-tests -d snippets/tests/"$level1"/"$level2"; then 9 | for exp in *.exp; do 10 | testbase=$(basename $exp .exp); 11 | echo -e "\nFAILURE $testbase"; 12 | diff -u $testbase.exp $testbase.out; 13 | done 14 | exit 1 15 | fi 16 | done 17 | done 18 | 19 | -------------------------------------------------------------------------------- /tests/1d/complex/complex_exp.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(4), dtype=dtype) 12 | print('\narray:\n', a) 13 | print('\nexponential:\n', np.exp(a)) 14 | 15 | b = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 16 | print('\narray:\n', b) 17 | print('\nexponential:\n', np.exp(b)) -------------------------------------------------------------------------------- /tests/1d/complex/complex_exp.py.exp: -------------------------------------------------------------------------------- 1 | 2 | array: 3 | array([0, 1, 2, 3], dtype=uint8) 4 | 5 | exponential: 6 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 7 | 8 | array: 9 | array([0, 1, 2, 3], dtype=int8) 10 | 11 | exponential: 12 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 13 | 14 | array: 15 | array([0, 1, 2, 3], dtype=uint16) 16 | 17 | exponential: 18 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 19 | 20 | array: 21 | array([0, 1, 2, 3], dtype=int16) 22 | 23 | exponential: 24 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 25 | 26 | array: 27 | array([0.0, 1.0, 2.0, 3.0], dtype=float64) 28 | 29 | exponential: 30 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 31 | 32 | array: 33 | array([0.0+0.0j, 1.0+0.0j, 2.0+0.0j, 3.0+0.0j], dtype=complex) 34 | 35 | exponential: 36 | array([1.0+0.0j, 2.718281828459045+0.0j, 7.38905609893065+0.0j, 20.08553692318767+0.0j], dtype=complex) 37 | 38 | array: 39 | array([0.0+0.0j, 0.0+1.0j, 2.0+2.0j, 3.0-3.0j], dtype=complex) 40 | 41 | exponential: 42 | array([1.0+0.0j, 0.5403023058681398+0.8414709848078965j, -3.074932320639359+6.71884969742825j, -19.88453084414699-2.834471132487004j], dtype=complex) 43 | -------------------------------------------------------------------------------- /tests/1d/complex/complex_sqrt.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(4), dtype=dtype) 12 | outtype = np.float if dtype is not np.complex else np.complex 13 | print('\narray:\n', a) 14 | print('\nsquare root:\n', np.sqrt(a, dtype=outtype)) 15 | 16 | b = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 17 | print('\narray:\n', b) 18 | print('\nsquare root:\n', np.sqrt(b, dtype=np.complex)) -------------------------------------------------------------------------------- /tests/1d/complex/complex_sqrt.py.exp: -------------------------------------------------------------------------------- 1 | 2 | array: 3 | array([0, 1, 2, 3], dtype=uint8) 4 | 5 | square root: 6 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 7 | 8 | array: 9 | array([0, 1, 2, 3], dtype=int8) 10 | 11 | square root: 12 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 13 | 14 | array: 15 | array([0, 1, 2, 3], dtype=uint16) 16 | 17 | square root: 18 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 19 | 20 | array: 21 | array([0, 1, 2, 3], dtype=int16) 22 | 23 | square root: 24 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 25 | 26 | array: 27 | array([0.0, 1.0, 2.0, 3.0], dtype=float64) 28 | 29 | square root: 30 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 31 | 32 | array: 33 | array([0.0+0.0j, 1.0+0.0j, 2.0+0.0j, 3.0+0.0j], dtype=complex) 34 | 35 | square root: 36 | array([0.0+0.0j, 1.0+0.0j, 1.414213562373095+0.0j, 1.732050807568877+0.0j], dtype=complex) 37 | 38 | array: 39 | array([0.0+0.0j, 0.0+1.0j, 2.0+2.0j, 3.0-3.0j], dtype=complex) 40 | 41 | square root: 42 | array([0.0+0.0j, 0.7071067811865476+0.7071067811865475j, 1.553773974030037+0.6435942529055827j, 1.902976705995016-0.7882387605032136j], dtype=complex) 43 | -------------------------------------------------------------------------------- /tests/1d/complex/imag_real.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(5), dtype=dtype) 12 | print('real part: ', np.real(a)) 13 | print('imaginary part: ', np.imag(a)) 14 | 15 | 16 | b = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 17 | print('real part: ', np.real(b)) 18 | print('imaginary part: ', np.imag(b)) 19 | 20 | -------------------------------------------------------------------------------- /tests/1d/complex/imag_real.py.exp: -------------------------------------------------------------------------------- 1 | real part: array([0, 1, 2, 3, 4], dtype=uint8) 2 | imaginary part: array([0, 0, 0, 0, 0], dtype=uint8) 3 | real part: array([0, 1, 2, 3, 4], dtype=int8) 4 | imaginary part: array([0, 0, 0, 0, 0], dtype=int8) 5 | real part: array([0, 1, 2, 3, 4], dtype=uint16) 6 | imaginary part: array([0, 0, 0, 0, 0], dtype=uint16) 7 | real part: array([0, 1, 2, 3, 4], dtype=int16) 8 | imaginary part: array([0, 0, 0, 0, 0], dtype=int16) 9 | real part: array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float64) 10 | imaginary part: array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=float64) 11 | real part: array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float64) 12 | imaginary part: array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=float64) 13 | real part: array([0.0, 0.0, 2.0, 3.0], dtype=float64) 14 | imaginary part: array([0.0, 1.0, 2.0, -3.0], dtype=float64) 15 | -------------------------------------------------------------------------------- /tests/1d/numpy/00smoke.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | 3 | print(np.ones(3)) 4 | -------------------------------------------------------------------------------- /tests/1d/numpy/00smoke.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0, 1.0, 1.0], dtype=float64) 2 | -------------------------------------------------------------------------------- /tests/1d/numpy/argminmax.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | 3 | # Adapted from https://docs.python.org/3.8/library/itertools.html#itertools.permutations 4 | def permutations(iterable, r=None): 5 | # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC 6 | # permutations(range(3)) --> 012 021 102 120 201 210 7 | pool = tuple(iterable) 8 | n = len(pool) 9 | r = n if r is None else r 10 | if r > n: 11 | return 12 | indices = list(range(n)) 13 | cycles = list(range(n, n-r, -1)) 14 | yield tuple(pool[i] for i in indices[:r]) 15 | while n: 16 | for i in reversed(range(r)): 17 | cycles[i] -= 1 18 | if cycles[i] == 0: 19 | indices[i:] = indices[i+1:] + indices[i:i+1] 20 | cycles[i] = n - i 21 | else: 22 | j = cycles[i] 23 | indices[i], indices[-j] = indices[-j], indices[i] 24 | yield tuple(pool[i] for i in indices[:r]) 25 | break 26 | else: 27 | return 28 | 29 | # Combinations expected to throw 30 | try: 31 | print(np.argmin([])) 32 | except ValueError: 33 | print("ValueError") 34 | 35 | try: 36 | print(np.argmax([])) 37 | except ValueError: 38 | print("ValueError") 39 | 40 | # Combinations expected to succeed 41 | print(np.argmin([1])) 42 | print(np.argmax([1])) 43 | print(np.argmin(np.array([1]))) 44 | print(np.argmax(np.array([1]))) 45 | 46 | print() 47 | print("max tests") 48 | for p in permutations((100,200,300)): 49 | m1 = np.argmax(p) 50 | m2 = np.argmax(np.array(p)) 51 | print(p, m1, m2) 52 | if m1 != m2 or p[m1] != max(p): 53 | print("FAIL", p, m1, m2, max(p)) 54 | 55 | print() 56 | print("min tests") 57 | for p in permutations((100,200,300)): 58 | m1 = np.argmin(p) 59 | m2 = np.argmin(np.array(p)) 60 | print(p, m1, m2) 61 | if m1 != m2 or p[m1] != min(p): 62 | print("FAIL", p, m1, m2, min(p)) 63 | -------------------------------------------------------------------------------- /tests/1d/numpy/argminmax.py.exp: -------------------------------------------------------------------------------- 1 | ValueError 2 | ValueError 3 | 0 4 | 0 5 | 0 6 | 0 7 | 8 | max tests 9 | (100, 200, 300) 2 2 10 | (100, 300, 200) 1 1 11 | (200, 100, 300) 2 2 12 | (200, 300, 100) 1 1 13 | (300, 100, 200) 0 0 14 | (300, 200, 100) 0 0 15 | 16 | min tests 17 | (100, 200, 300) 0 0 18 | (100, 300, 200) 0 0 19 | (200, 100, 300) 1 1 20 | (200, 300, 100) 2 2 21 | (300, 100, 200) 1 1 22 | (300, 200, 100) 2 2 23 | -------------------------------------------------------------------------------- /tests/1d/numpy/compare.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | 3 | a = np.array([1, 2, 3, 4, 5], dtype=np.uint8) 4 | b = np.array([5, 4, 3, 2, 1], dtype=np.float) 5 | print(np.minimum(a, b)) 6 | print(np.maximum(a, b)) 7 | print(np.maximum(1, 5.5)) 8 | 9 | a = np.array(range(9), dtype=np.uint8) 10 | print(np.clip(a, 3, 7)) 11 | 12 | b = 3 * np.ones(len(a), dtype=np.float) 13 | print(np.clip(a, b, 7)) 14 | -------------------------------------------------------------------------------- /tests/1d/numpy/compare.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0, 2.0, 3.0, 2.0, 1.0], dtype=float64) 2 | array([5.0, 4.0, 3.0, 4.0, 5.0], dtype=float64) 3 | 5.5 4 | array([3, 3, 3, 3, 4, 5, 6, 7, 7], dtype=uint8) 5 | array([3.0, 3.0, 3.0, 3.0, 4.0, 5.0, 6.0, 7.0, 7.0], dtype=float64) 6 | -------------------------------------------------------------------------------- /tests/1d/numpy/convolve.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | try: 4 | from ulab import numpy as np 5 | except ImportError: 6 | import numpy as np 7 | 8 | x = np.array((1,2,3)) 9 | y = np.array((1,10,100,1000)) 10 | result = (np.convolve(x, y)) 11 | ref_result = np.array([1, 12, 123, 1230, 2300, 3000],dtype=np.float) 12 | cmp_result = [] 13 | for p,q in zip(list(result), list(ref_result)): 14 | cmp_result.append(math.isclose(p, q, rel_tol=1e-06, abs_tol=1e-06)) 15 | print(cmp_result) 16 | -------------------------------------------------------------------------------- /tests/1d/numpy/convolve.py.exp: -------------------------------------------------------------------------------- 1 | [True, True, True, True, True, True] 2 | -------------------------------------------------------------------------------- /tests/1d/numpy/fft.py: -------------------------------------------------------------------------------- 1 | import math 2 | try: 3 | from ulab import numpy as np 4 | use_ulab = True 5 | except ImportError: 6 | import numpy as np 7 | use_ulab = False 8 | 9 | x = np.linspace(-np.pi, np.pi, num=8) 10 | y = np.sin(x) 11 | 12 | if use_ulab: 13 | if 'real' in dir(np): 14 | a = np.fft.fft(y) 15 | c = np.real(np.fft.ifft(a)) 16 | else: 17 | a, b = np.fft.fft(y) 18 | c, d = np.fft.ifft(a, b) 19 | # c should be equal to y 20 | cmp_result = [] 21 | for p,q in zip(list(y), list(c)): 22 | cmp_result.append(math.isclose(p, q, rel_tol=1e-09, abs_tol=1e-09)) 23 | print(cmp_result) 24 | 25 | z = np.zeros(len(x)) 26 | if 'real' in dir(np): 27 | a = np.fft.fft(y) 28 | c = np.real(np.fft.ifft(a)) 29 | else: 30 | a, b = np.fft.fft(y, z) 31 | c, d = np.fft.ifft(a, b) 32 | # c should be equal to y 33 | cmp_result = [] 34 | for p,q in zip(list(y), list(c)): 35 | cmp_result.append(math.isclose(p, q, rel_tol=1e-09, abs_tol=1e-09)) 36 | print(cmp_result) 37 | 38 | else: 39 | a = np.fft.fft(y) 40 | c = np.fft.ifft(a) 41 | # c should be equal to y 42 | cmp_result = [] 43 | for p,q in zip(list(y), list(c.real)): 44 | cmp_result.append(math.isclose(p, q, rel_tol=1e-09, abs_tol=1e-09)) 45 | print(cmp_result) 46 | -------------------------------------------------------------------------------- /tests/1d/numpy/fft.py.exp: -------------------------------------------------------------------------------- 1 | [True, True, True, True, True, True, True, True] 2 | [True, True, True, True, True, True, True, True] 3 | -------------------------------------------------------------------------------- /tests/1d/numpy/gc.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | import gc 3 | 4 | data = np.ones(1000)[6:-6] 5 | print(sum(data)) 6 | print(data) 7 | 8 | gc.collect() 9 | 10 | print(sum(data)) 11 | print(data) -------------------------------------------------------------------------------- /tests/1d/numpy/gc.py.exp: -------------------------------------------------------------------------------- 1 | 988.0 2 | array([1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0], dtype=float64) 3 | 988.0 4 | array([1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0], dtype=float64) 5 | -------------------------------------------------------------------------------- /tests/1d/numpy/interp.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | x = np.array([1, 2, 3, 4, 5]) 7 | xp = np.array([1, 2, 3, 4]) 8 | fp = np.array([1, 2, 3, 4]) 9 | print(np.interp(x, xp, fp)) 10 | print(np.interp(x, xp, fp, left=0.0)) 11 | print(np.interp(x, xp, fp, right=10.0)) 12 | print(np.interp(x, xp, fp, left=0.0, right=10.0)) 13 | -------------------------------------------------------------------------------- /tests/1d/numpy/interp.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0, 2.0, 3.0, 4.0, 4.0], dtype=float64) 2 | array([1.0, 2.0, 3.0, 4.0, 4.0], dtype=float64) 3 | array([1.0, 2.0, 3.0, 4.0, 10.0], dtype=float64) 4 | array([1.0, 2.0, 3.0, 4.0, 10.0], dtype=float64) 5 | -------------------------------------------------------------------------------- /tests/1d/numpy/optimize.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | try: 4 | from ulab import scipy as spy 5 | except ImportError: 6 | import scipy as spy 7 | 8 | def f(x): 9 | return x**2 - 2.0 10 | 11 | ref_result = 1.4142135623715149 12 | result = (spy.optimize.bisect(f, 1.0, 3.0)) 13 | print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6)) 14 | 15 | ref_result = -7.105427357601002e-15 16 | result = spy.optimize.fmin(f, 3.0, fatol=1e-15) 17 | print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6)) 18 | 19 | ref_result = -7.105427357601002e-15 20 | result = spy.optimize.fmin(f, 3.0, xatol=1e-8, fatol=1e-15, maxiter=500) 21 | print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6)) 22 | 23 | ref_result = 1.41421826342255 24 | result = (spy.optimize.newton(f, 3.0, tol=0.001, rtol=0.01)) 25 | print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6)) 26 | 27 | result = (spy.optimize.newton(f, 3.0, tol=0.001, rtol=0.01, maxiter=100)) 28 | print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6)) 29 | -------------------------------------------------------------------------------- /tests/1d/numpy/optimize.py.exp: -------------------------------------------------------------------------------- 1 | True 2 | True 3 | True 4 | True 5 | True 6 | -------------------------------------------------------------------------------- /tests/1d/numpy/poly.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | try: 4 | from ulab import numpy as np 5 | except ImportError: 6 | import numpy as np 7 | 8 | p = [1, 1, 1, 0] 9 | x = [0, 1, 2, 3, 4] 10 | result = np.polyval(p, x) 11 | ref_result = np.array([0, 3, 14, 39, 84]) 12 | for i in range(len(x)): 13 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9)) 14 | 15 | a = np.array(x) 16 | result = np.polyval(p, a) 17 | ref_result = np.array([0, 3, 14, 39, 84]) 18 | for i in range(len(x)): 19 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9)) 20 | 21 | # linear fit 22 | x = np.linspace(-10, 10, 20) 23 | y = 1.5*x + 3 24 | result = np.polyfit(x, y, 1) 25 | ref_result = np.array([ 1.5, 3.0]) 26 | for i in range(2): 27 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9)) 28 | 29 | # 2nd degree fit 30 | x = np.linspace(-10, 10, 20) 31 | y = x*x*2.5 - x*0.5 + 1.2 32 | result = np.polyfit(x, y, 2) 33 | ref_result = np.array([2.5, -0.5, 1.2]) 34 | for i in range(3): 35 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9)) 36 | 37 | # 3rd degree fit 38 | x = np.linspace(-10, 10, 20) 39 | y = x*x*x*1.255 + x*x*1.0 - x*0.75 + 0.0 40 | result = np.polyfit(x, y, 3) 41 | ref_result = np.array([1.255, 1.0, -0.75, 0.0]) 42 | for i in range(4): 43 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9)) 44 | 45 | # 4th degree fit 46 | x = np.linspace(-10, 10, 20) 47 | y = x*x*x*x + x*x*x*1.255 + x*x*1.0 - x*0.75 + 0.0 48 | result = np.polyfit(x, y, 4) 49 | ref_result = np.array([1.0, 1.255, 1.0, -0.75, 0.0]) 50 | for i in range(5): 51 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9)) 52 | -------------------------------------------------------------------------------- /tests/1d/numpy/poly.py.exp: -------------------------------------------------------------------------------- 1 | True 2 | True 3 | True 4 | True 5 | True 6 | True 7 | True 8 | True 9 | True 10 | True 11 | True 12 | True 13 | True 14 | True 15 | True 16 | True 17 | True 18 | True 19 | True 20 | True 21 | True 22 | True 23 | True 24 | True 25 | -------------------------------------------------------------------------------- /tests/1d/numpy/slicing.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | for num in range(1,4): 7 | for start in range(-num, num+1): 8 | for end in range(-num, num+1): 9 | for stride in (-3, -2, -1, 1, 2, 3): 10 | l = list(range(num)) 11 | a = np.array(l, dtype=np.int8) 12 | sl = l[start:end:stride] 13 | ll = len(sl) 14 | try: 15 | sa = list(a[start:end:stride]) 16 | except IndexError as e: 17 | sa = str(e) 18 | print("%2d [% d:% d:% d] %-24r %-24r%s" % ( 19 | num, start, end, stride, sl, sa, " ***" if sa != sl else "")) 20 | 21 | a[start:end:stride] = np.ones(len(sl)) * -1 22 | print("%2d [% d:% d:% d] %r" % ( 23 | num, start, end, stride, list(a))) 24 | -------------------------------------------------------------------------------- /tests/1d/numpy/slicing2.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | a = np.array(range(9), dtype=np.float) 7 | print("a:\t", list(a)) 8 | print("a < 5:\t", list(a[a < 5])) 9 | -------------------------------------------------------------------------------- /tests/1d/numpy/slicing2.py.exp: -------------------------------------------------------------------------------- 1 | a: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] 2 | a < 5: [0.0, 1.0, 2.0, 3.0, 4.0] 3 | -------------------------------------------------------------------------------- /tests/1d/numpy/sum.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | 3 | r = range(15) 4 | 5 | a = np.array(r, dtype=np.uint8) 6 | print(np.sum(a)) 7 | 8 | a = np.array(r, dtype=np.int8) 9 | print(np.sum(a)) 10 | 11 | a = np.array(r, dtype=np.uint16) 12 | print(np.sum(a)) 13 | 14 | a = np.array(r, dtype=np.int16) 15 | print(np.sum(a)) 16 | 17 | a = np.array(r, dtype=np.float) 18 | print(np.sum(a)) 19 | 20 | a = np.array([False] + [True]*15, dtype=np.bool) 21 | print(np.sum(a)) 22 | -------------------------------------------------------------------------------- /tests/1d/numpy/sum.py.exp: -------------------------------------------------------------------------------- 1 | 105 2 | 105 3 | 105 4 | 105 5 | 105.0 6 | 15 7 | -------------------------------------------------------------------------------- /tests/1d/numpy/trapz.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | x = np.linspace(0, 9, num=10) 7 | y = x*x 8 | print(np.trapz(y)) 9 | print(np.trapz(y, x=x)) 10 | -------------------------------------------------------------------------------- /tests/1d/numpy/trapz.py.exp: -------------------------------------------------------------------------------- 1 | 244.5 2 | 244.5 3 | -------------------------------------------------------------------------------- /tests/1d/numpy/universal_functions.py.exp: -------------------------------------------------------------------------------- 1 | True 2 | True 3 | True 4 | True 5 | True 6 | True 7 | True 8 | True 9 | True 10 | True 11 | True 12 | 180.0 13 | 3.141592653589793 14 | 3.0 15 | 4.0 16 | 1.772453850905516 17 | 2.718281828459045 18 | 1.0 19 | 1.0 20 | 1.0 21 | True 22 | [True, True, True, True] 23 | [True, True, True, True, True] 24 | [True, True, True, True, True] 25 | [True, True, True, True, True] 26 | [True, True, True, True, True] 27 | [True, True, True, True, True] 28 | [True, True, True, True, True] 29 | [True, True, True, True, True] 30 | [True, True, True, True, True] 31 | [True, True, True, True, True] 32 | [True, True, True, True] 33 | [True, True, True] 34 | -------------------------------------------------------------------------------- /tests/2d/complex/binary_op.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | n = 5 9 | a = np.array(range(n), dtype=np.complex) 10 | c = np.array(range(n), dtype=np.complex) 11 | 12 | print(a == c) 13 | print(a != c) 14 | print() 15 | 16 | c = np.array(range(n), dtype=np.complex) * 1j 17 | print(a == c) 18 | print(a != c) 19 | print() 20 | 21 | for dtype in dtypes: 22 | b = np.array(range(n), dtype=dtype) 23 | print(b == a) 24 | print(b != a) 25 | print() 26 | 27 | -------------------------------------------------------------------------------- /tests/2d/complex/binary_op.py.exp: -------------------------------------------------------------------------------- 1 | array([True, True, True, True, True], dtype=bool) 2 | array([False, False, False, False, False], dtype=bool) 3 | 4 | array([True, False, False, False, False], dtype=bool) 5 | array([False, True, True, True, True], dtype=bool) 6 | 7 | array([True, True, True, True, True], dtype=bool) 8 | array([False, False, False, False, False], dtype=bool) 9 | 10 | array([True, True, True, True, True], dtype=bool) 11 | array([False, False, False, False, False], dtype=bool) 12 | 13 | array([True, True, True, True, True], dtype=bool) 14 | array([False, False, False, False, False], dtype=bool) 15 | 16 | array([True, True, True, True, True], dtype=bool) 17 | array([False, False, False, False, False], dtype=bool) 18 | 19 | array([True, True, True, True, True], dtype=bool) 20 | array([False, False, False, False, False], dtype=bool) 21 | 22 | -------------------------------------------------------------------------------- /tests/2d/complex/complex_exp.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(4), dtype=dtype) 12 | b = a.reshape((2, 2)) 13 | print('\narray:\n', a) 14 | print('\nexponential:\n', np.exp(a)) 15 | print('\narray:\n', b) 16 | print('\nexponential:\n', np.exp(b)) 17 | 18 | b = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 19 | print('\narray:\n', b) 20 | print('\nexponential:\n', np.exp(b)) 21 | 22 | b = np.array([[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]], dtype=np.complex) 23 | print('\narray:\n', b) 24 | print('\nexponential:\n', np.exp(b)) -------------------------------------------------------------------------------- /tests/2d/complex/complex_exp.py.exp: -------------------------------------------------------------------------------- 1 | 2 | array: 3 | array([0, 1, 2, 3], dtype=uint8) 4 | 5 | exponential: 6 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 7 | 8 | array: 9 | array([[0, 1], 10 | [2, 3]], dtype=uint8) 11 | 12 | exponential: 13 | array([[1.0, 2.718281828459045], 14 | [7.38905609893065, 20.08553692318767]], dtype=float64) 15 | 16 | array: 17 | array([0, 1, 2, 3], dtype=int8) 18 | 19 | exponential: 20 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 21 | 22 | array: 23 | array([[0, 1], 24 | [2, 3]], dtype=int8) 25 | 26 | exponential: 27 | array([[1.0, 2.718281828459045], 28 | [7.38905609893065, 20.08553692318767]], dtype=float64) 29 | 30 | array: 31 | array([0, 1, 2, 3], dtype=uint16) 32 | 33 | exponential: 34 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 35 | 36 | array: 37 | array([[0, 1], 38 | [2, 3]], dtype=uint16) 39 | 40 | exponential: 41 | array([[1.0, 2.718281828459045], 42 | [7.38905609893065, 20.08553692318767]], dtype=float64) 43 | 44 | array: 45 | array([0, 1, 2, 3], dtype=int16) 46 | 47 | exponential: 48 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 49 | 50 | array: 51 | array([[0, 1], 52 | [2, 3]], dtype=int16) 53 | 54 | exponential: 55 | array([[1.0, 2.718281828459045], 56 | [7.38905609893065, 20.08553692318767]], dtype=float64) 57 | 58 | array: 59 | array([0.0, 1.0, 2.0, 3.0], dtype=float64) 60 | 61 | exponential: 62 | array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767], dtype=float64) 63 | 64 | array: 65 | array([[0.0, 1.0], 66 | [2.0, 3.0]], dtype=float64) 67 | 68 | exponential: 69 | array([[1.0, 2.718281828459045], 70 | [7.38905609893065, 20.08553692318767]], dtype=float64) 71 | 72 | array: 73 | array([0.0+0.0j, 1.0+0.0j, 2.0+0.0j, 3.0+0.0j], dtype=complex) 74 | 75 | exponential: 76 | array([1.0+0.0j, 2.718281828459045+0.0j, 7.38905609893065+0.0j, 20.08553692318767+0.0j], dtype=complex) 77 | 78 | array: 79 | array([[0.0+0.0j, 1.0+0.0j], 80 | [2.0+0.0j, 3.0+0.0j]], dtype=complex) 81 | 82 | exponential: 83 | array([[1.0+0.0j, 2.718281828459045+0.0j], 84 | [7.38905609893065+0.0j, 20.08553692318767+0.0j]], dtype=complex) 85 | 86 | array: 87 | array([0.0+0.0j, 0.0+1.0j, 2.0+2.0j, 3.0-3.0j], dtype=complex) 88 | 89 | exponential: 90 | array([1.0+0.0j, 0.5403023058681398+0.8414709848078965j, -3.074932320639359+6.71884969742825j, -19.88453084414699-2.834471132487004j], dtype=complex) 91 | 92 | array: 93 | array([[0.0+0.0j, 0.0+1.0j, 2.0+2.0j, 3.0-3.0j], 94 | [0.0+0.0j, 0.0+1.0j, 2.0+2.0j, 3.0-3.0j]], dtype=complex) 95 | 96 | exponential: 97 | array([[1.0+0.0j, 0.5403023058681398+0.8414709848078965j, -3.074932320639359+6.71884969742825j, -19.88453084414699-2.834471132487004j], 98 | [1.0+0.0j, 0.5403023058681398+0.8414709848078965j, -3.074932320639359+6.71884969742825j, -19.88453084414699-2.834471132487004j]], dtype=complex) 99 | -------------------------------------------------------------------------------- /tests/2d/complex/complex_sqrt.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(4), dtype=dtype) 12 | b = a.reshape((2, 2)) 13 | outtype = np.float if dtype is not np.complex else np.complex 14 | print('\narray:\n', a) 15 | print('\nsquare root:\n', np.sqrt(a, dtype=outtype)) 16 | print('\narray:\n', b) 17 | print('\nsquare root:\n', np.sqrt(b, dtype=outtype)) 18 | 19 | b = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 20 | print('\narray:\n', b) 21 | print('\nsquare root:\n', np.sqrt(b, dtype=np.complex)) 22 | 23 | b = np.array([[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]], dtype=np.complex) 24 | print('\narray:\n', b) 25 | print('\nsquare root:\n', np.sqrt(b, dtype=np.complex)) 26 | -------------------------------------------------------------------------------- /tests/2d/complex/complex_sqrt.py.exp: -------------------------------------------------------------------------------- 1 | 2 | array: 3 | array([0, 1, 2, 3], dtype=uint8) 4 | 5 | square root: 6 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 7 | 8 | array: 9 | array([[0, 1], 10 | [2, 3]], dtype=uint8) 11 | 12 | square root: 13 | array([[0.0, 1.0], 14 | [1.414213562373095, 1.732050807568877]], dtype=float64) 15 | 16 | array: 17 | array([0, 1, 2, 3], dtype=int8) 18 | 19 | square root: 20 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 21 | 22 | array: 23 | array([[0, 1], 24 | [2, 3]], dtype=int8) 25 | 26 | square root: 27 | array([[0.0, 1.0], 28 | [1.414213562373095, 1.732050807568877]], dtype=float64) 29 | 30 | array: 31 | array([0, 1, 2, 3], dtype=uint16) 32 | 33 | square root: 34 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 35 | 36 | array: 37 | array([[0, 1], 38 | [2, 3]], dtype=uint16) 39 | 40 | square root: 41 | array([[0.0, 1.0], 42 | [1.414213562373095, 1.732050807568877]], dtype=float64) 43 | 44 | array: 45 | array([0, 1, 2, 3], dtype=int16) 46 | 47 | square root: 48 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 49 | 50 | array: 51 | array([[0, 1], 52 | [2, 3]], dtype=int16) 53 | 54 | square root: 55 | array([[0.0, 1.0], 56 | [1.414213562373095, 1.732050807568877]], dtype=float64) 57 | 58 | array: 59 | array([0.0, 1.0, 2.0, 3.0], dtype=float64) 60 | 61 | square root: 62 | array([0.0, 1.0, 1.414213562373095, 1.732050807568877], dtype=float64) 63 | 64 | array: 65 | array([[0.0, 1.0], 66 | [2.0, 3.0]], dtype=float64) 67 | 68 | square root: 69 | array([[0.0, 1.0], 70 | [1.414213562373095, 1.732050807568877]], dtype=float64) 71 | 72 | array: 73 | array([0.0+0.0j, 1.0+0.0j, 2.0+0.0j, 3.0+0.0j], dtype=complex) 74 | 75 | square root: 76 | array([0.0+0.0j, 1.0+0.0j, 1.414213562373095+0.0j, 1.732050807568877+0.0j], dtype=complex) 77 | 78 | array: 79 | array([[0.0+0.0j, 1.0+0.0j], 80 | [2.0+0.0j, 3.0+0.0j]], dtype=complex) 81 | 82 | square root: 83 | array([[0.0+0.0j, 1.0+0.0j], 84 | [1.414213562373095+0.0j, 1.732050807568877+0.0j]], dtype=complex) 85 | 86 | array: 87 | array([0.0+0.0j, 0.0+1.0j, 2.0+2.0j, 3.0-3.0j], dtype=complex) 88 | 89 | square root: 90 | array([0.0+0.0j, 0.7071067811865476+0.7071067811865475j, 1.553773974030037+0.6435942529055827j, 1.902976705995016-0.7882387605032136j], dtype=complex) 91 | 92 | array: 93 | array([[0.0+0.0j, 0.0+1.0j, 2.0+2.0j, 3.0-3.0j], 94 | [0.0+0.0j, 0.0+1.0j, 2.0+2.0j, 3.0-3.0j]], dtype=complex) 95 | 96 | square root: 97 | array([[0.0+0.0j, 0.7071067811865476+0.7071067811865475j, 1.553773974030037+0.6435942529055827j, 1.902976705995016-0.7882387605032136j], 98 | [0.0+0.0j, 0.7071067811865476+0.7071067811865475j, 1.553773974030037+0.6435942529055827j, 1.902976705995016-0.7882387605032136j]], dtype=complex) 99 | -------------------------------------------------------------------------------- /tests/2d/complex/conjugate.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 7 | 8 | for dtype in dtypes: 9 | print(np.conjugate(np.array(range(5), dtype=dtype))) 10 | 11 | a = np.array([1, 2+2j, 3-3j, 4j], dtype=np.complex) 12 | print(np.conjugate(a)) -------------------------------------------------------------------------------- /tests/2d/complex/conjugate.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 2, 3, 4], dtype=uint8) 2 | array([0, 1, 2, 3, 4], dtype=int8) 3 | array([0, 1, 2, 3, 4], dtype=uint16) 4 | array([0, 1, 2, 3, 4], dtype=int16) 5 | array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float64) 6 | array([0.0+-0.0j, 1.0+-0.0j, 2.0+-0.0j, 3.0+-0.0j, 4.0+-0.0j], dtype=complex) 7 | array([1.0+-0.0j, 2.0-2.0j, 3.0+3.0j, 0.0-4.0j], dtype=complex) 8 | -------------------------------------------------------------------------------- /tests/2d/complex/imag_real.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(4), dtype=dtype) 12 | b = a.reshape((2, 2)) 13 | print('\narray:\n', a) 14 | print('\nreal part:\n', np.real(a)) 15 | print('\nimaginary part:\n', np.imag(a)) 16 | print('\narray:\n', b) 17 | print('\nreal part:\n', np.real(b)) 18 | print('\nimaginary part:\n', np.imag(b), '\n') 19 | 20 | 21 | b = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 22 | print('\nreal part:\n', np.real(b)) 23 | print('\nimaginary part:\n', np.imag(b)) 24 | 25 | b = np.array([[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]], dtype=np.complex) 26 | print('\nreal part:\n', np.real(b)) 27 | print('\nimaginary part:\n', np.imag(b)) 28 | 29 | -------------------------------------------------------------------------------- /tests/2d/complex/imag_real.py.exp: -------------------------------------------------------------------------------- 1 | 2 | array: 3 | array([0, 1, 2, 3], dtype=uint8) 4 | 5 | real part: 6 | array([0, 1, 2, 3], dtype=uint8) 7 | 8 | imaginary part: 9 | array([0, 0, 0, 0], dtype=uint8) 10 | 11 | array: 12 | array([[0, 1], 13 | [2, 3]], dtype=uint8) 14 | 15 | real part: 16 | array([[0, 1], 17 | [2, 3]], dtype=uint8) 18 | 19 | imaginary part: 20 | array([[0, 0], 21 | [0, 0]], dtype=uint8) 22 | 23 | 24 | array: 25 | array([0, 1, 2, 3], dtype=int8) 26 | 27 | real part: 28 | array([0, 1, 2, 3], dtype=int8) 29 | 30 | imaginary part: 31 | array([0, 0, 0, 0], dtype=int8) 32 | 33 | array: 34 | array([[0, 1], 35 | [2, 3]], dtype=int8) 36 | 37 | real part: 38 | array([[0, 1], 39 | [2, 3]], dtype=int8) 40 | 41 | imaginary part: 42 | array([[0, 0], 43 | [0, 0]], dtype=int8) 44 | 45 | 46 | array: 47 | array([0, 1, 2, 3], dtype=uint16) 48 | 49 | real part: 50 | array([0, 1, 2, 3], dtype=uint16) 51 | 52 | imaginary part: 53 | array([0, 0, 0, 0], dtype=uint16) 54 | 55 | array: 56 | array([[0, 1], 57 | [2, 3]], dtype=uint16) 58 | 59 | real part: 60 | array([[0, 1], 61 | [2, 3]], dtype=uint16) 62 | 63 | imaginary part: 64 | array([[0, 0], 65 | [0, 0]], dtype=uint16) 66 | 67 | 68 | array: 69 | array([0, 1, 2, 3], dtype=int16) 70 | 71 | real part: 72 | array([0, 1, 2, 3], dtype=int16) 73 | 74 | imaginary part: 75 | array([0, 0, 0, 0], dtype=int16) 76 | 77 | array: 78 | array([[0, 1], 79 | [2, 3]], dtype=int16) 80 | 81 | real part: 82 | array([[0, 1], 83 | [2, 3]], dtype=int16) 84 | 85 | imaginary part: 86 | array([[0, 0], 87 | [0, 0]], dtype=int16) 88 | 89 | 90 | array: 91 | array([0.0, 1.0, 2.0, 3.0], dtype=float64) 92 | 93 | real part: 94 | array([0.0, 1.0, 2.0, 3.0], dtype=float64) 95 | 96 | imaginary part: 97 | array([0.0, 0.0, 0.0, 0.0], dtype=float64) 98 | 99 | array: 100 | array([[0.0, 1.0], 101 | [2.0, 3.0]], dtype=float64) 102 | 103 | real part: 104 | array([[0.0, 1.0], 105 | [2.0, 3.0]], dtype=float64) 106 | 107 | imaginary part: 108 | array([[0.0, 0.0], 109 | [0.0, 0.0]], dtype=float64) 110 | 111 | 112 | array: 113 | array([0.0+0.0j, 1.0+0.0j, 2.0+0.0j, 3.0+0.0j], dtype=complex) 114 | 115 | real part: 116 | array([0.0, 1.0, 2.0, 3.0], dtype=float64) 117 | 118 | imaginary part: 119 | array([0.0, 0.0, 0.0, 0.0], dtype=float64) 120 | 121 | array: 122 | array([[0.0+0.0j, 1.0+0.0j], 123 | [2.0+0.0j, 3.0+0.0j]], dtype=complex) 124 | 125 | real part: 126 | array([[0.0, 1.0], 127 | [2.0, 3.0]], dtype=float64) 128 | 129 | imaginary part: 130 | array([[0.0, 0.0], 131 | [0.0, 0.0]], dtype=float64) 132 | 133 | 134 | real part: 135 | array([0.0, 0.0, 2.0, 3.0], dtype=float64) 136 | 137 | imaginary part: 138 | array([0.0, 1.0, 2.0, -3.0], dtype=float64) 139 | 140 | real part: 141 | array([[0.0, 0.0, 2.0, 3.0], 142 | [0.0, 0.0, 2.0, 3.0]], dtype=float64) 143 | 144 | imaginary part: 145 | array([[0.0, 1.0, 2.0, -3.0], 146 | [0.0, 1.0, 2.0, -3.0]], dtype=float64) 147 | -------------------------------------------------------------------------------- /tests/2d/complex/sort_complex.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 7 | 8 | for dtype in dtypes: 9 | print(np.sort_complex(np.array(range(5, 0, -1), dtype=dtype))) 10 | # these should all return an empty complex array 11 | print(np.sort_complex(np.array(range(5, 0, 1), dtype=dtype))) 12 | 13 | print() 14 | n = 6 15 | a = np.array(range(n, 0, -1)) * 1j 16 | b = np.array([1] * n) 17 | print(np.sort_complex(a + b)) 18 | 19 | a = np.array(range(n)) * 1j 20 | b = np.array([1] * n) 21 | print(np.sort_complex(a + b)) 22 | 23 | print() 24 | a = np.array([0, -3j, 1+2j, 1-2j, 2j], dtype=np.complex) 25 | print(np.sort_complex(a)) 26 | 27 | a = np.array([0, 3j, 1-2j, 1+2j, -2j], dtype=np.complex) 28 | print(np.sort_complex(a)) 29 | -------------------------------------------------------------------------------- /tests/2d/complex/sort_complex.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) 2 | array([], dtype=complex) 3 | array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) 4 | array([], dtype=complex) 5 | array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) 6 | array([], dtype=complex) 7 | array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) 8 | array([], dtype=complex) 9 | array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) 10 | array([], dtype=complex) 11 | array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) 12 | array([], dtype=complex) 13 | 14 | array([1.0+1.0j, 1.0+2.0j, 1.0+3.0j, 1.0+4.0j, 1.0+5.0j, 1.0+6.0j], dtype=complex) 15 | array([1.0+0.0j, 1.0+1.0j, 1.0+2.0j, 1.0+3.0j, 1.0+4.0j, 1.0+5.0j], dtype=complex) 16 | 17 | array([-0.0-3.0j, 0.0+0.0j, 0.0+2.0j, 1.0-2.0j, 1.0+2.0j], dtype=complex) 18 | array([-0.0-2.0j, 0.0+0.0j, 0.0+3.0j, 1.0-2.0j, 1.0+2.0j], dtype=complex) 19 | -------------------------------------------------------------------------------- /tests/2d/numpy/00smoke.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | 3 | print(np.eye(3)) 4 | -------------------------------------------------------------------------------- /tests/2d/numpy/00smoke.py.exp: -------------------------------------------------------------------------------- 1 | array([[1.0, 0.0, 0.0], 2 | [0.0, 1.0, 0.0], 3 | [0.0, 0.0, 1.0]], dtype=float64) 4 | -------------------------------------------------------------------------------- /tests/2d/numpy/and.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 7 | 8 | for dtype_a in dtypes: 9 | a = np.array(range(5), dtype=dtype_a) 10 | for dtype_b in dtypes: 11 | b = np.array(range(250, 255), dtype=dtype_b) 12 | try: 13 | print('a & b: ', a & b) 14 | except Exception as e: 15 | print(e) 16 | 17 | b = np.array([False, True, False, True, False], dtype=np.bool) 18 | try: 19 | print('a & b (bool): ', a & b) 20 | except Exception as e: 21 | print(e) 22 | -------------------------------------------------------------------------------- /tests/2d/numpy/and.py.exp: -------------------------------------------------------------------------------- 1 | a & b: array([0, 1, 0, 1, 4], dtype=uint8) 2 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 3 | a & b: array([0, 1, 0, 1, 4], dtype=uint16) 4 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 5 | a & b (bool): array([0, 1, 0, 1, 0], dtype=uint8) 6 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 7 | a & b: array([0, 1, 0, 1, 4], dtype=int8) 8 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 9 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 10 | a & b (bool): array([0, 1, 0, 1, 0], dtype=int16) 11 | a & b: array([0, 1, 0, 1, 4], dtype=uint16) 12 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 13 | a & b: array([0, 1, 0, 1, 4], dtype=uint16) 14 | dtype of int32 is not supported 15 | a & b (bool): array([0, 1, 0, 1, 0], dtype=uint16) 16 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 17 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 18 | dtype of int32 is not supported 19 | a & b: array([0, 1, 0, 1, 4], dtype=int16) 20 | a & b (bool): array([0, 1, 0, 1, 0], dtype=int16) 21 | -------------------------------------------------------------------------------- /tests/2d/numpy/any_all.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | 3 | a = np.array(range(12)).reshape((3, 4)) 4 | 5 | print(np.all(a)) 6 | print(np.all(a, axis=0)) 7 | print(np.all(a, axis=1)) 8 | 9 | print(np.any(a)) 10 | print(np.any(a, axis=0)) 11 | print(np.any(a, axis=1)) -------------------------------------------------------------------------------- /tests/2d/numpy/any_all.py.exp: -------------------------------------------------------------------------------- 1 | False 2 | array([False, True, True, True], dtype=bool) 3 | array([False, True, True], dtype=bool) 4 | True 5 | array([True, True, True, True], dtype=bool) 6 | array([True, True, True], dtype=bool) 7 | -------------------------------------------------------------------------------- /tests/2d/numpy/arange.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype in dtypes: 9 | print(np.arange(10, dtype=dtype)) 10 | print(np.arange(2, 10, dtype=dtype)) 11 | print(np.arange(2, 10, 3, dtype=dtype)) 12 | # test empty range 13 | print(np.arange(0, 0, dtype=dtype)) 14 | 15 | # test for ZeroDivisionError exception 16 | try: 17 | np.arange(0, 10, 0) 18 | except ZeroDivisionError as e: 19 | print('ZeroDivisionError: ', e) 20 | 21 | # test for NAN length exception 22 | try: 23 | np.arange(0, np.nan) 24 | except ValueError as e: 25 | print('ValueError: ', e) 26 | -------------------------------------------------------------------------------- /tests/2d/numpy/arange.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8) 2 | array([2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8) 3 | array([2, 5, 8], dtype=uint8) 4 | array([], dtype=uint8) 5 | array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int8) 6 | array([2, 3, 4, 5, 6, 7, 8, 9], dtype=int8) 7 | array([2, 5, 8], dtype=int8) 8 | array([], dtype=int8) 9 | array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint16) 10 | array([2, 3, 4, 5, 6, 7, 8, 9], dtype=uint16) 11 | array([2, 5, 8], dtype=uint16) 12 | array([], dtype=uint16) 13 | array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int16) 14 | array([2, 3, 4, 5, 6, 7, 8, 9], dtype=int16) 15 | array([2, 5, 8], dtype=int16) 16 | array([], dtype=int16) 17 | array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64) 18 | array([2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64) 19 | array([2.0, 5.0, 8.0], dtype=float64) 20 | array([], dtype=float64) 21 | ZeroDivisionError: divide by zero 22 | ValueError: arange: cannot compute length 23 | -------------------------------------------------------------------------------- /tests/2d/numpy/asarray.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype in dtypes: 9 | a = np.ones((2, 2), dtype=dtype) 10 | print() 11 | for _dtype in dtypes: 12 | b = np.asarray(a, dtype=_dtype) 13 | print('a: ', a) 14 | print('b: ', b) 15 | print('a is b: {}\n'.format(a is b)) 16 | -------------------------------------------------------------------------------- /tests/2d/numpy/bitwise_and.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | 7 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 8 | test_values1 = (0, 1, 0, 1, 2, 5) 9 | test_values2 = (0, 1, 1, 0, 2, 7) 10 | 11 | 12 | for dtype1 in dtypes: 13 | x1 = np.array(test_values1, dtype=dtype1) 14 | for dtype2 in dtypes: 15 | x2 = np.array(test_values2, dtype=dtype2) 16 | print(np.bitwise_and(x1, x2)) 17 | -------------------------------------------------------------------------------- /tests/2d/numpy/bitwise_and.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 0, 0, 2, 5], dtype=uint8) 2 | array([0, 1, 0, 0, 2, 5], dtype=int16) 3 | array([0, 1, 0, 0, 2, 5], dtype=uint16) 4 | array([0, 1, 0, 0, 2, 5], dtype=int16) 5 | array([0, 1, 0, 0, 2, 5], dtype=int16) 6 | array([0, 1, 0, 0, 2, 5], dtype=int8) 7 | array([0, 1, 0, 0, 2, 5], dtype=uint16) 8 | array([0, 1, 0, 0, 2, 5], dtype=int16) 9 | array([0, 1, 0, 0, 2, 5], dtype=uint16) 10 | array([0, 1, 0, 0, 2, 5], dtype=uint16) 11 | array([0, 1, 0, 0, 2, 5], dtype=uint16) 12 | array([0, 1, 0, 0, 2, 5], dtype=int16) 13 | array([0, 1, 0, 0, 2, 5], dtype=int16) 14 | array([0, 1, 0, 0, 2, 5], dtype=int16) 15 | array([0, 1, 0, 0, 2, 5], dtype=int16) 16 | array([0, 1, 0, 0, 2, 5], dtype=int16) 17 | -------------------------------------------------------------------------------- /tests/2d/numpy/bitwise_or.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | 7 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 8 | test_values1 = (0, 1, 0, 1, 2, 5) 9 | test_values2 = (0, 1, 1, 0, 2, 7) 10 | 11 | 12 | for dtype1 in dtypes: 13 | x1 = np.array(test_values1, dtype=dtype1) 14 | for dtype2 in dtypes: 15 | x2 = np.array(test_values2, dtype=dtype2) 16 | print(np.bitwise_or(x1, x2)) 17 | -------------------------------------------------------------------------------- /tests/2d/numpy/bitwise_or.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 1, 1, 2, 7], dtype=uint8) 2 | array([0, 1, 1, 1, 2, 7], dtype=int16) 3 | array([0, 1, 1, 1, 2, 7], dtype=uint16) 4 | array([0, 1, 1, 1, 2, 7], dtype=int16) 5 | array([0, 1, 1, 1, 2, 7], dtype=int16) 6 | array([0, 1, 1, 1, 2, 7], dtype=int8) 7 | array([0, 1, 1, 1, 2, 7], dtype=uint16) 8 | array([0, 1, 1, 1, 2, 7], dtype=int16) 9 | array([0, 1, 1, 1, 2, 7], dtype=uint16) 10 | array([0, 1, 1, 1, 2, 7], dtype=uint16) 11 | array([0, 1, 1, 1, 2, 7], dtype=uint16) 12 | array([0, 1, 1, 1, 2, 7], dtype=int16) 13 | array([0, 1, 1, 1, 2, 7], dtype=int16) 14 | array([0, 1, 1, 1, 2, 7], dtype=int16) 15 | array([0, 1, 1, 1, 2, 7], dtype=int16) 16 | array([0, 1, 1, 1, 2, 7], dtype=int16) 17 | -------------------------------------------------------------------------------- /tests/2d/numpy/bitwise_xor.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | 7 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 8 | 9 | for dtype1 in dtypes: 10 | x1 = np.array(range(5), dtype=dtype1) 11 | for dtype2 in dtypes: 12 | x2 = np.array(range(5, 0, -1), dtype=dtype2) 13 | 14 | print(np.bitwise_xor(x1, x2)) 15 | -------------------------------------------------------------------------------- /tests/2d/numpy/bitwise_xor.py.exp: -------------------------------------------------------------------------------- 1 | array([5, 5, 1, 1, 5], dtype=uint8) 2 | array([5, 5, 1, 1, 5], dtype=int16) 3 | array([5, 5, 1, 1, 5], dtype=uint16) 4 | array([5, 5, 1, 1, 5], dtype=int16) 5 | array([5, 5, 1, 1, 5], dtype=int16) 6 | array([5, 5, 1, 1, 5], dtype=int8) 7 | array([5, 5, 1, 1, 5], dtype=uint16) 8 | array([5, 5, 1, 1, 5], dtype=int16) 9 | array([5, 5, 1, 1, 5], dtype=uint16) 10 | array([5, 5, 1, 1, 5], dtype=uint16) 11 | array([5, 5, 1, 1, 5], dtype=uint16) 12 | array([5, 5, 1, 1, 5], dtype=int16) 13 | array([5, 5, 1, 1, 5], dtype=int16) 14 | array([5, 5, 1, 1, 5], dtype=int16) 15 | array([5, 5, 1, 1, 5], dtype=int16) 16 | array([5, 5, 1, 1, 5], dtype=int16) 17 | -------------------------------------------------------------------------------- /tests/2d/numpy/buffer.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | def print_as_buffer(a): 7 | print(len(memoryview(a)), list(memoryview(a))) 8 | print_as_buffer(np.ones(3)) 9 | print_as_buffer(np.zeros(3)) 10 | print_as_buffer(np.eye(4)) 11 | print_as_buffer(np.ones(1, dtype=np.int8)) 12 | print_as_buffer(np.ones(2, dtype=np.uint8)) 13 | print_as_buffer(np.ones(3, dtype=np.int16)) 14 | print_as_buffer(np.ones(4, dtype=np.uint16)) 15 | print_as_buffer(np.ones(5, dtype=np.float)) 16 | print_as_buffer(np.linspace(0, 1, 9)) 17 | 18 | -------------------------------------------------------------------------------- /tests/2d/numpy/buffer.py.exp: -------------------------------------------------------------------------------- 1 | 3 [1.0, 1.0, 1.0] 2 | 3 [0.0, 0.0, 0.0] 3 | 16 [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0] 4 | 1 [1] 5 | 2 [1, 1] 6 | 3 [1, 1, 1] 7 | 4 [1, 1, 1, 1] 8 | 5 [1.0, 1.0, 1.0, 1.0, 1.0] 9 | 9 [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0] 10 | -------------------------------------------------------------------------------- /tests/2d/numpy/cholesky.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | 3 | a = np.array([[1, 2], [2, 5]]) 4 | print(np.linalg.cholesky(a)) 5 | 6 | b = a = np.array([[25, 15, -5], [15, 18, 0], [-5, 0, 11]]) 7 | print(np.linalg.cholesky(b)) 8 | 9 | c = np.array([[18, 22, 54, 42], [22, 70, 86, 62], [54, 86, 174, 134], [42, 62, 134, 106]]) 10 | print(np.linalg.cholesky(c)) 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /tests/2d/numpy/cholesky.py.exp: -------------------------------------------------------------------------------- 1 | array([[1.0, 0.0], 2 | [2.0, 1.0]], dtype=float64) 3 | array([[5.0, 0.0, 0.0], 4 | [3.0, 3.0, 0.0], 5 | [-1.0, 1.0, 3.0]], dtype=float64) 6 | array([[4.242640687119285, 0.0, 0.0, 0.0], 7 | [5.185449728701349, 6.565905201197403, 0.0, 0.0], 8 | [12.72792206135786, 3.046038495400855, 1.649742247909068, 0.0], 9 | [9.899494936611665, 1.624553864213789, 1.849711005231386, 1.392621247645583]], dtype=float64) 10 | -------------------------------------------------------------------------------- /tests/2d/numpy/concatenate.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | # test input types; the following should raise ValueErrors 7 | objects = [([1, 2], [3, 4]), 8 | ((1, 2), (3, 4)), 9 | (1, 2, 3)] 10 | 11 | for obj in objects: 12 | try: 13 | np.concatenate(obj) 14 | except ValueError as e: 15 | print('ValueError: {}; failed with object {}\n'.format(e, obj)) 16 | 17 | 18 | a = np.array([1,2,3], dtype=np.float) 19 | b = np.array([4,5,6], dtype=np.float) 20 | 21 | print(np.concatenate((a,b))) 22 | print(np.concatenate((a,b), axis=0)) 23 | 24 | a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.float) 25 | b = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.float) 26 | 27 | print(np.concatenate((a,b), axis=0)) 28 | print(np.concatenate((a,b), axis=1)) 29 | print(np.concatenate((b,a), axis=0)) 30 | print(np.concatenate((b,a), axis=1)) 31 | -------------------------------------------------------------------------------- /tests/2d/numpy/concatenate.py.exp: -------------------------------------------------------------------------------- 1 | ValueError: only ndarrays can be concatenated; failed with object ([1, 2], [3, 4]) 2 | 3 | ValueError: only ndarrays can be concatenated; failed with object ((1, 2), (3, 4)) 4 | 5 | ValueError: only ndarrays can be concatenated; failed with object (1, 2, 3) 6 | 7 | array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=float64) 8 | array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=float64) 9 | array([[1.0, 2.0, 3.0], 10 | [4.0, 5.0, 6.0], 11 | [7.0, 8.0, 9.0], 12 | [1.0, 2.0, 3.0], 13 | [4.0, 5.0, 6.0], 14 | [7.0, 8.0, 9.0]], dtype=float64) 15 | array([[1.0, 2.0, 3.0, 1.0, 2.0, 3.0], 16 | [4.0, 5.0, 6.0, 4.0, 5.0, 6.0], 17 | [7.0, 8.0, 9.0, 7.0, 8.0, 9.0]], dtype=float64) 18 | array([[1.0, 2.0, 3.0], 19 | [4.0, 5.0, 6.0], 20 | [7.0, 8.0, 9.0], 21 | [1.0, 2.0, 3.0], 22 | [4.0, 5.0, 6.0], 23 | [7.0, 8.0, 9.0]], dtype=float64) 24 | array([[1.0, 2.0, 3.0, 1.0, 2.0, 3.0], 25 | [4.0, 5.0, 6.0, 4.0, 5.0, 6.0], 26 | [7.0, 8.0, 9.0, 7.0, 8.0, 9.0]], dtype=float64) 27 | -------------------------------------------------------------------------------- /tests/2d/numpy/delete.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | np.set_printoptions(threshold=200) 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(25), dtype=dtype).reshape((5,5)) 12 | print(np.delete(a, [1, 2], axis=0)) 13 | print(np.delete(a, [1, 2], axis=1)) 14 | print(np.delete(a, [], axis=1)) 15 | print(np.delete(a, [1, 5, 10])) 16 | print(np.delete(a, [])) 17 | 18 | for dtype in dtypes: 19 | a = np.array(range(25), dtype=dtype).reshape((5,5)) 20 | print(np.delete(a, 2, axis=0)) 21 | print(np.delete(a, 2, axis=1)) 22 | print(np.delete(a, 2)) 23 | 24 | for dtype in dtypes: 25 | a = np.array(range(25), dtype=dtype).reshape((5,5)) 26 | print(np.delete(a, -3, axis=0)) 27 | print(np.delete(a, -3, axis=1)) 28 | print(np.delete(a, -3)) 29 | -------------------------------------------------------------------------------- /tests/2d/numpy/diag.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | a = np.arange(25).reshape((5,5)) 7 | 8 | print(np.diag(a)) 9 | print(np.diag(a, k=2)) 10 | print(np.diag(a, k=-2)) 11 | print(np.diag(a, k=10)) 12 | print(np.diag(a, k=-10)) 13 | 14 | a = np.arange(4) 15 | 16 | print(np.diag(a)) 17 | print(np.diag(a, k=2)) 18 | print(np.diag(a, k=-2)) 19 | 20 | 21 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 22 | 23 | for dtype in dtypes: 24 | a = np.array(range(4), dtype=dtype) 25 | print(np.diag(a)) 26 | 27 | for dtype in dtypes: 28 | a = np.array(range(16), dtype=dtype).reshape((4,4)) 29 | print(np.diag(a)) 30 | -------------------------------------------------------------------------------- /tests/2d/numpy/diag.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 6, 12, 18, 24], dtype=int16) 2 | array([2, 8, 14], dtype=int16) 3 | array([10, 16, 22], dtype=int16) 4 | array([], dtype=int16) 5 | array([], dtype=int16) 6 | array([[0, 0, 0, 0], 7 | [0, 1, 0, 0], 8 | [0, 0, 2, 0], 9 | [0, 0, 0, 3]], dtype=int16) 10 | array([[0, 0, 0, 0, 0, 0], 11 | [0, 0, 0, 1, 0, 0], 12 | [0, 0, 0, 0, 2, 0], 13 | [0, 0, 0, 0, 0, 3], 14 | [0, 0, 0, 0, 0, 0], 15 | [0, 0, 0, 0, 0, 0]], dtype=int16) 16 | array([[0, 0, 0, 0, 0, 0], 17 | [0, 0, 0, 0, 0, 0], 18 | [0, 0, 0, 0, 0, 0], 19 | [0, 1, 0, 0, 0, 0], 20 | [0, 0, 2, 0, 0, 0], 21 | [0, 0, 0, 3, 0, 0]], dtype=int16) 22 | array([[0, 0, 0, 0], 23 | [0, 1, 0, 0], 24 | [0, 0, 2, 0], 25 | [0, 0, 0, 3]], dtype=uint8) 26 | array([[0, 0, 0, 0], 27 | [0, 1, 0, 0], 28 | [0, 0, 2, 0], 29 | [0, 0, 0, 3]], dtype=int8) 30 | array([[0, 0, 0, 0], 31 | [0, 1, 0, 0], 32 | [0, 0, 2, 0], 33 | [0, 0, 0, 3]], dtype=uint16) 34 | array([[0, 0, 0, 0], 35 | [0, 1, 0, 0], 36 | [0, 0, 2, 0], 37 | [0, 0, 0, 3]], dtype=int16) 38 | array([[0.0, 0.0, 0.0, 0.0], 39 | [0.0, 1.0, 0.0, 0.0], 40 | [0.0, 0.0, 2.0, 0.0], 41 | [0.0, 0.0, 0.0, 3.0]], dtype=float64) 42 | array([0, 5, 10, 15], dtype=uint8) 43 | array([0, 5, 10, 15], dtype=int8) 44 | array([0, 5, 10, 15], dtype=uint16) 45 | array([0, 5, 10, 15], dtype=int16) 46 | array([0.0, 5.0, 10.0, 15.0], dtype=float64) 47 | -------------------------------------------------------------------------------- /tests/2d/numpy/eye.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | print(np.ones(3)) 9 | print(np.ones((3,3))) 10 | 11 | print(np.eye(3)) 12 | print(np.eye(3, M=4)) 13 | print(np.eye(3, M=4, k=0)) 14 | print(np.eye(3, M=4, k=-1)) 15 | print(np.eye(3, M=4, k=-2)) 16 | print(np.eye(3, M=4, k=-3)) 17 | print(np.eye(3, M=4, k=1)) 18 | print(np.eye(3, M=4, k=2)) 19 | print(np.eye(3, M=4, k=3)) 20 | print(np.eye(4, M=4)) 21 | print(np.eye(4, M=3, k=0)) 22 | print(np.eye(4, M=3, k=-1)) 23 | print(np.eye(4, M=3, k=-2)) 24 | print(np.eye(4, M=3, k=-3)) 25 | print(np.eye(4, M=3, k=1)) 26 | print(np.eye(4, M=3, k=2)) 27 | print(np.eye(4, M=3, k=3)) 28 | 29 | for dtype in dtypes: 30 | print(np.eye(3, dtype=dtype)) -------------------------------------------------------------------------------- /tests/2d/numpy/eye.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0, 1.0, 1.0], dtype=float64) 2 | array([[1.0, 1.0, 1.0], 3 | [1.0, 1.0, 1.0], 4 | [1.0, 1.0, 1.0]], dtype=float64) 5 | array([[1.0, 0.0, 0.0], 6 | [0.0, 1.0, 0.0], 7 | [0.0, 0.0, 1.0]], dtype=float64) 8 | array([[1.0, 0.0, 0.0, 0.0], 9 | [0.0, 1.0, 0.0, 0.0], 10 | [0.0, 0.0, 1.0, 0.0]], dtype=float64) 11 | array([[1.0, 0.0, 0.0, 0.0], 12 | [0.0, 1.0, 0.0, 0.0], 13 | [0.0, 0.0, 1.0, 0.0]], dtype=float64) 14 | array([[0.0, 0.0, 0.0, 0.0], 15 | [1.0, 0.0, 0.0, 0.0], 16 | [0.0, 1.0, 0.0, 0.0]], dtype=float64) 17 | array([[0.0, 0.0, 0.0, 0.0], 18 | [0.0, 0.0, 0.0, 0.0], 19 | [1.0, 0.0, 0.0, 0.0]], dtype=float64) 20 | array([[0.0, 0.0, 0.0, 0.0], 21 | [0.0, 0.0, 0.0, 0.0], 22 | [0.0, 0.0, 0.0, 0.0]], dtype=float64) 23 | array([[0.0, 1.0, 0.0, 0.0], 24 | [0.0, 0.0, 1.0, 0.0], 25 | [0.0, 0.0, 0.0, 1.0]], dtype=float64) 26 | array([[0.0, 0.0, 1.0, 0.0], 27 | [0.0, 0.0, 0.0, 1.0], 28 | [0.0, 0.0, 0.0, 0.0]], dtype=float64) 29 | array([[0.0, 0.0, 0.0, 1.0], 30 | [0.0, 0.0, 0.0, 0.0], 31 | [0.0, 0.0, 0.0, 0.0]], dtype=float64) 32 | array([[1.0, 0.0, 0.0, 0.0], 33 | [0.0, 1.0, 0.0, 0.0], 34 | [0.0, 0.0, 1.0, 0.0], 35 | [0.0, 0.0, 0.0, 1.0]], dtype=float64) 36 | array([[1.0, 0.0, 0.0], 37 | [0.0, 1.0, 0.0], 38 | [0.0, 0.0, 1.0], 39 | [0.0, 0.0, 0.0]], dtype=float64) 40 | array([[0.0, 0.0, 0.0], 41 | [1.0, 0.0, 0.0], 42 | [0.0, 1.0, 0.0], 43 | [0.0, 0.0, 1.0]], dtype=float64) 44 | array([[0.0, 0.0, 0.0], 45 | [0.0, 0.0, 0.0], 46 | [1.0, 0.0, 0.0], 47 | [0.0, 1.0, 0.0]], dtype=float64) 48 | array([[0.0, 0.0, 0.0], 49 | [0.0, 0.0, 0.0], 50 | [0.0, 0.0, 0.0], 51 | [1.0, 0.0, 0.0]], dtype=float64) 52 | array([[0.0, 1.0, 0.0], 53 | [0.0, 0.0, 1.0], 54 | [0.0, 0.0, 0.0], 55 | [0.0, 0.0, 0.0]], dtype=float64) 56 | array([[0.0, 0.0, 1.0], 57 | [0.0, 0.0, 0.0], 58 | [0.0, 0.0, 0.0], 59 | [0.0, 0.0, 0.0]], dtype=float64) 60 | array([[0.0, 0.0, 0.0], 61 | [0.0, 0.0, 0.0], 62 | [0.0, 0.0, 0.0], 63 | [0.0, 0.0, 0.0]], dtype=float64) 64 | array([[1, 0, 0], 65 | [0, 1, 0], 66 | [0, 0, 1]], dtype=uint8) 67 | array([[1, 0, 0], 68 | [0, 1, 0], 69 | [0, 0, 1]], dtype=int8) 70 | array([[1, 0, 0], 71 | [0, 1, 0], 72 | [0, 0, 1]], dtype=uint16) 73 | array([[1, 0, 0], 74 | [0, 1, 0], 75 | [0, 0, 1]], dtype=int16) 76 | array([[1.0, 0.0, 0.0], 77 | [0.0, 1.0, 0.0], 78 | [0.0, 0.0, 1.0]], dtype=float64) 79 | -------------------------------------------------------------------------------- /tests/2d/numpy/full.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype in dtypes: 9 | print(np.full((2, 4), 3, dtype=dtype)) -------------------------------------------------------------------------------- /tests/2d/numpy/full.py.exp: -------------------------------------------------------------------------------- 1 | array([[3, 3, 3, 3], 2 | [3, 3, 3, 3]], dtype=uint8) 3 | array([[3, 3, 3, 3], 4 | [3, 3, 3, 3]], dtype=int8) 5 | array([[3, 3, 3, 3], 6 | [3, 3, 3, 3]], dtype=uint16) 7 | array([[3, 3, 3, 3], 8 | [3, 3, 3, 3]], dtype=int16) 9 | array([[3.0, 3.0, 3.0, 3.0], 10 | [3.0, 3.0, 3.0, 3.0]], dtype=float64) 11 | -------------------------------------------------------------------------------- /tests/2d/numpy/initialisation.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype1 in dtypes: 9 | for dtype2 in dtypes: 10 | print(np.array(np.array(range(5), dtype=dtype1), dtype=dtype2)) -------------------------------------------------------------------------------- /tests/2d/numpy/initialisation.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 2, 3, 4], dtype=uint8) 2 | array([0, 1, 2, 3, 4], dtype=int8) 3 | array([0, 1, 2, 3, 4], dtype=uint16) 4 | array([0, 1, 2, 3, 4], dtype=int16) 5 | array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float64) 6 | array([0, 1, 2, 3, 4], dtype=uint8) 7 | array([0, 1, 2, 3, 4], dtype=int8) 8 | array([0, 1, 2, 3, 4], dtype=uint16) 9 | array([0, 1, 2, 3, 4], dtype=int16) 10 | array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float64) 11 | array([0, 1, 2, 3, 4], dtype=uint8) 12 | array([0, 1, 2, 3, 4], dtype=int8) 13 | array([0, 1, 2, 3, 4], dtype=uint16) 14 | array([0, 1, 2, 3, 4], dtype=int16) 15 | array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float64) 16 | array([0, 1, 2, 3, 4], dtype=uint8) 17 | array([0, 1, 2, 3, 4], dtype=int8) 18 | array([0, 1, 2, 3, 4], dtype=uint16) 19 | array([0, 1, 2, 3, 4], dtype=int16) 20 | array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float64) 21 | array([0, 1, 2, 3, 4], dtype=uint8) 22 | array([0, 1, 2, 3, 4], dtype=int8) 23 | array([0, 1, 2, 3, 4], dtype=uint16) 24 | array([0, 1, 2, 3, 4], dtype=int16) 25 | array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float64) 26 | -------------------------------------------------------------------------------- /tests/2d/numpy/isinf.py: -------------------------------------------------------------------------------- 1 | 2 | from ulab import numpy as np 3 | 4 | print('isinf(0): ', np.isinf(0)) 5 | 6 | a = np.array([1, 2, np.nan]) 7 | print('\n' + '='*20) 8 | print('a:\n', a) 9 | print('\nisinf(a):\n', np.isinf(a)) 10 | 11 | b = np.array([1, 2, np.inf]) 12 | print('\n' + '='*20) 13 | print('b:\n', b) 14 | print('\nisinf(b):\n', np.isinf(b)) 15 | 16 | c = np.array([1, 2, 3], dtype=np.uint16) 17 | print('\n' + '='*20) 18 | print('c:\n', c) 19 | print('\nisinf(c):\n', np.isinf(c)) 20 | 21 | d = np.eye(5) * 1e999 22 | print('\n' + '='*20) 23 | print('d:\n', d) 24 | print('\nisinf(d):\n', np.isinf(d)) -------------------------------------------------------------------------------- /tests/2d/numpy/isinf.py.exp: -------------------------------------------------------------------------------- 1 | isinf(0): False 2 | 3 | ==================== 4 | a: 5 | array([1.0, 2.0, nan], dtype=float64) 6 | 7 | isinf(a): 8 | array([False, False, False], dtype=bool) 9 | 10 | ==================== 11 | b: 12 | array([1.0, 2.0, inf], dtype=float64) 13 | 14 | isinf(b): 15 | array([False, False, True], dtype=bool) 16 | 17 | ==================== 18 | c: 19 | array([1, 2, 3], dtype=uint16) 20 | 21 | isinf(c): 22 | array([False, False, False], dtype=bool) 23 | 24 | ==================== 25 | d: 26 | array([[inf, nan, nan, nan, nan], 27 | [nan, inf, nan, nan, nan], 28 | [nan, nan, inf, nan, nan], 29 | [nan, nan, nan, inf, nan], 30 | [nan, nan, nan, nan, inf]], dtype=float64) 31 | 32 | isinf(d): 33 | array([[True, False, False, False, False], 34 | [False, True, False, False, False], 35 | [False, False, True, False, False], 36 | [False, False, False, True, False], 37 | [False, False, False, False, True]], dtype=bool) 38 | -------------------------------------------------------------------------------- /tests/2d/numpy/left_shift.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | np.set_printoptions(threshold=100) 6 | 7 | 8 | shift_values = ( 9 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 10 | (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 11 | (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), 12 | ) 13 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 14 | 15 | 16 | for shift_value in shift_values: 17 | for dtype1 in dtypes: 18 | x1 = np.array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=dtype1) 19 | for dtype2 in dtypes: 20 | x2 = np.array(shift_value, dtype=dtype2) 21 | print(np.left_shift(x1, x2)) 22 | -------------------------------------------------------------------------------- /tests/2d/numpy/left_shift.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint8) 2 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 3 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16) 4 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 5 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 6 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int8) 7 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16) 8 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 9 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16) 10 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int8) 11 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16) 12 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 13 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 14 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 15 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 16 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 17 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint8) 18 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 19 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint16) 20 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 21 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 22 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int8) 23 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint16) 24 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 25 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint16) 26 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int8) 27 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint16) 28 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 29 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 30 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 31 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 32 | array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16) 33 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint8) 34 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 35 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint16) 36 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 37 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 38 | array([0, 4, 8, 16, 32, 64, -128, 12, 20, 28, 44, 52], dtype=int8) 39 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint16) 40 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 41 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint16) 42 | array([0, 4, 8, 16, 32, 64, -128, 12, 20, 28, 44, 52], dtype=int8) 43 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint16) 44 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 45 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 46 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 47 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 48 | array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16) 49 | -------------------------------------------------------------------------------- /tests/2d/numpy/linalg.py.exp: -------------------------------------------------------------------------------- 1 | True 2 | True 3 | True 4 | True 5 | True 6 | True 7 | True 8 | True 9 | True 10 | True 11 | True 12 | True 13 | True 14 | True 15 | True 16 | True 17 | True 18 | True 19 | True 20 | True 21 | True 22 | True 23 | True 24 | True 25 | True 26 | True 27 | True 28 | True 29 | True 30 | True 31 | True 32 | True 33 | True 34 | True 35 | True 36 | True 37 | True 38 | True 39 | True 40 | True 41 | True 42 | True 43 | True 44 | True 45 | True 46 | True 47 | True 48 | True 49 | True 50 | True 51 | True 52 | True 53 | True 54 | True 55 | True 56 | True 57 | True 58 | True 59 | True 60 | True 61 | True 62 | True 63 | True 64 | True 65 | True 66 | True 67 | True 68 | True 69 | True 70 | -------------------------------------------------------------------------------- /tests/2d/numpy/linspace.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype in dtypes: 9 | print(np.linspace(0, 10, num=5, dtype=dtype)) 10 | print(np.linspace(0, 10, num=5, endpoint=True, dtype=dtype)) 11 | -------------------------------------------------------------------------------- /tests/2d/numpy/linspace.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 2, 5, 7, 10], dtype=uint8) 2 | array([0, 2, 5, 7, 10], dtype=uint8) 3 | array([0, 2, 5, 7, 10], dtype=int8) 4 | array([0, 2, 5, 7, 10], dtype=int8) 5 | array([0, 2, 5, 7, 10], dtype=uint16) 6 | array([0, 2, 5, 7, 10], dtype=uint16) 7 | array([0, 2, 5, 7, 10], dtype=int16) 8 | array([0, 2, 5, 7, 10], dtype=int16) 9 | array([0.0, 2.5, 5.0, 7.5, 10.0], dtype=float64) 10 | array([0.0, 2.5, 5.0, 7.5, 10.0], dtype=float64) 11 | -------------------------------------------------------------------------------- /tests/2d/numpy/load_save.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype in dtypes: 9 | a = np.array(range(25), dtype=dtype) 10 | b = a.reshape((5, 5)) 11 | np.save('out.npy', a) 12 | print(np.load('out.npy')) 13 | np.save('out.npy', b) 14 | print(np.load('out.npy')) 15 | -------------------------------------------------------------------------------- /tests/2d/numpy/load_save.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 2, ..., 22, 23, 24], dtype=uint8) 2 | array([[0, 1, 2, 3, 4], 3 | [5, 6, 7, 8, 9], 4 | [10, 11, 12, 13, 14], 5 | [15, 16, 17, 18, 19], 6 | [20, 21, 22, 23, 24]], dtype=uint8) 7 | array([0, 1, 2, ..., 22, 23, 24], dtype=int8) 8 | array([[0, 1, 2, 3, 4], 9 | [5, 6, 7, 8, 9], 10 | [10, 11, 12, 13, 14], 11 | [15, 16, 17, 18, 19], 12 | [20, 21, 22, 23, 24]], dtype=int8) 13 | array([0, 1, 2, ..., 22, 23, 24], dtype=uint16) 14 | array([[0, 1, 2, 3, 4], 15 | [5, 6, 7, 8, 9], 16 | [10, 11, 12, 13, 14], 17 | [15, 16, 17, 18, 19], 18 | [20, 21, 22, 23, 24]], dtype=uint16) 19 | array([0, 1, 2, ..., 22, 23, 24], dtype=int16) 20 | array([[0, 1, 2, 3, 4], 21 | [5, 6, 7, 8, 9], 22 | [10, 11, 12, 13, 14], 23 | [15, 16, 17, 18, 19], 24 | [20, 21, 22, 23, 24]], dtype=int16) 25 | array([0.0, 1.0, 2.0, ..., 22.0, 23.0, 24.0], dtype=float64) 26 | array([[0.0, 1.0, 2.0, 3.0, 4.0], 27 | [5.0, 6.0, 7.0, 8.0, 9.0], 28 | [10.0, 11.0, 12.0, 13.0, 14.0], 29 | [15.0, 16.0, 17.0, 18.0, 19.0], 30 | [20.0, 21.0, 22.0, 23.0, 24.0]], dtype=float64) 31 | -------------------------------------------------------------------------------- /tests/2d/numpy/loadtxt.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 7 | 8 | a = np.array(range(8)).reshape((2, 4)) 9 | np.savetxt('loadtxt.dat', a, header='test file data') 10 | 11 | print(np.loadtxt('loadtxt.dat')) 12 | print() 13 | 14 | for dtype in dtypes: 15 | print(np.loadtxt('loadtxt.dat', dtype=dtype)) 16 | print() 17 | 18 | np.savetxt('loadtxt.dat', a, delimiter=',', header='test file data') 19 | 20 | print(np.loadtxt('loadtxt.dat', delimiter=',')) 21 | print() 22 | 23 | np.savetxt('loadtxt.dat', a, delimiter=',', comments='!', header='test file data') 24 | 25 | print(np.loadtxt('loadtxt.dat', delimiter=',', comments='!')) 26 | print() 27 | print(np.loadtxt('loadtxt.dat', delimiter=',', comments='!', usecols=1)) 28 | print() 29 | print(np.loadtxt('loadtxt.dat', delimiter=',', comments='!', usecols=(0, 1))) 30 | print() 31 | 32 | a = np.array(range(36)).reshape((9, 4)) 33 | np.savetxt('loadtxt.dat', a, header='9 data rows and a comment') 34 | print(np.loadtxt('loadtxt.dat', max_rows=5)) 35 | 36 | print() 37 | print(np.loadtxt('loadtxt.dat', skiprows=5, dtype=np.uint16)) 38 | -------------------------------------------------------------------------------- /tests/2d/numpy/loadtxt.py.exp: -------------------------------------------------------------------------------- 1 | array([[0.0, 1.0, 2.0, 3.0], 2 | [4.0, 5.0, 6.0, 7.0]], dtype=float64) 3 | 4 | array([[0, 1, 2, 3], 5 | [4, 5, 6, 7]], dtype=uint8) 6 | 7 | array([[0, 1, 2, 3], 8 | [4, 5, 6, 7]], dtype=int8) 9 | 10 | array([[0, 1, 2, 3], 11 | [4, 5, 6, 7]], dtype=uint16) 12 | 13 | array([[0, 1, 2, 3], 14 | [4, 5, 6, 7]], dtype=int16) 15 | 16 | array([[0.0, 1.0, 2.0, 3.0], 17 | [4.0, 5.0, 6.0, 7.0]], dtype=float64) 18 | 19 | array([[0.0, 1.0, 2.0, 3.0], 20 | [4.0, 5.0, 6.0, 7.0]], dtype=float64) 21 | 22 | array([[1.0], 23 | [5.0]], dtype=float64) 24 | 25 | array([[0.0, 1.0], 26 | [4.0, 5.0]], dtype=float64) 27 | 28 | array([[0.0, 1.0, 2.0, 3.0], 29 | [4.0, 5.0, 6.0, 7.0], 30 | [8.0, 9.0, 10.0, 11.0], 31 | [12.0, 13.0, 14.0, 15.0]], dtype=float64) 32 | 33 | array([[16, 17, 18, 19], 34 | [20, 21, 22, 23], 35 | [24, 25, 26, 27], 36 | [28, 29, 30, 31], 37 | [32, 33, 34, 35]], dtype=uint16) 38 | -------------------------------------------------------------------------------- /tests/2d/numpy/logspace.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype in dtypes: 9 | print(np.logspace(0, 10, num=5, endpoint=False, dtype=dtype)) 10 | print(np.logspace(0, 10, num=5, endpoint=True, dtype=dtype)) -------------------------------------------------------------------------------- /tests/2d/numpy/logspace.py.exp: -------------------------------------------------------------------------------- 1 | array([1, 100, 16, 64, 0], dtype=uint8) 2 | array([1, 60, 160, 120, 0], dtype=uint8) 3 | array([1, 100, 16, 64, 0], dtype=int8) 4 | array([1, 60, -96, 120, 0], dtype=int8) 5 | array([1, 100, 10000, 16960, 57600], dtype=uint16) 6 | array([1, 316, 34464, 34424, 0], dtype=uint16) 7 | array([1, 100, 10000, 16960, -7936], dtype=int16) 8 | array([1, 316, -31072, -31112, 0], dtype=int16) 9 | array([1.0, 100.0, 10000.0, 1000000.0, 100000000.0], dtype=float64) 10 | array([1.0, 316.227766016838, 100000.0, 31622776.6016838, 10000000000.0], dtype=float64) 11 | -------------------------------------------------------------------------------- /tests/2d/numpy/methods.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | a = np.array([1, 2, 3, 4], dtype=np.int8) 7 | b = a.copy() 8 | print(b) 9 | a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.int16) 10 | b = a.copy() 11 | print(b) 12 | a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.float) 13 | b = a.copy() 14 | print(b) 15 | print(a.dtype) 16 | print(a.flatten()) 17 | print(np.array([1,2,3], dtype=np.uint8).itemsize) 18 | print(np.array([1,2,3], dtype=np.uint16).itemsize) 19 | print(np.array([1,2,3], dtype=np.int8).itemsize) 20 | print(np.array([1,2,3], dtype=np.int16).itemsize) 21 | print(np.array([1,2,3], dtype=np.float).itemsize) 22 | print(np.array([1,2,3], dtype=np.float).shape) 23 | print(np.array([[1],[2],[3]], dtype=np.float).shape) 24 | print(np.array([[1],[2],[3]], dtype=np.float).reshape((1,3))) 25 | print(np.array([[1],[2],[3]]).size) 26 | print(np.array([1,2,3], dtype=np.float).size) 27 | print(np.array([1,2,3], dtype=np.uint8).tobytes()) 28 | print(np.array([1,2,3], dtype=np.int8).tobytes()) 29 | print(np.array([1,2,3], dtype=np.float).transpose().shape) 30 | print(np.array([[1],[2],[3]], dtype=np.float).transpose().shape) 31 | a = np.array([1, 2, 3, 4, 5, 6], dtype=np.uint8) 32 | b = a.byteswap(inplace=False) 33 | print(a) 34 | print(b) 35 | c = a.byteswap(inplace=True) 36 | print(a) 37 | print(c) 38 | a = np.array([1, 2, 3, 4, 5, 6], dtype=np.uint16) 39 | b = a.byteswap(inplace=False) 40 | print(a) 41 | print(b) 42 | c = a.byteswap(inplace=True) 43 | print(a) 44 | print(c) 45 | -------------------------------------------------------------------------------- /tests/2d/numpy/methods.py.exp: -------------------------------------------------------------------------------- 1 | array([1, 2, 3, 4], dtype=int8) 2 | array([[1, 2, 3], 3 | [4, 5, 6], 4 | [7, 8, 9]], dtype=int16) 5 | array([[1.0, 2.0, 3.0], 6 | [4.0, 5.0, 6.0], 7 | [7.0, 8.0, 9.0]], dtype=float64) 8 | 100 9 | array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64) 10 | 1 11 | 2 12 | 1 13 | 2 14 | 8 15 | (3,) 16 | (3, 1) 17 | array([[1.0, 2.0, 3.0]], dtype=float64) 18 | 3 19 | 3 20 | bytearray(b'\x01\x02\x03') 21 | bytearray(b'\x01\x02\x03') 22 | (3,) 23 | (1, 3) 24 | array([1, 2, 3, 4, 5, 6], dtype=uint8) 25 | array([1, 2, 3, 4, 5, 6], dtype=uint8) 26 | array([1, 2, 3, 4, 5, 6], dtype=uint8) 27 | array([1, 2, 3, 4, 5, 6], dtype=uint8) 28 | array([1, 2, 3, 4, 5, 6], dtype=uint16) 29 | array([256, 512, 768, 1024, 1280, 1536], dtype=uint16) 30 | array([256, 512, 768, 1024, 1280, 1536], dtype=uint16) 31 | array([256, 512, 768, 1024, 1280, 1536], dtype=uint16) 32 | -------------------------------------------------------------------------------- /tests/2d/numpy/nonzero.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | array = np.array(range(16)).reshape((4,4)) 7 | print(array) 8 | print(array < 5) 9 | print(np.nonzero(array < 5)) 10 | 11 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 12 | 13 | for dtype in dtypes: 14 | array = (np.arange(2, 12, 3, dtype=dtype)).reshape((2,2)) - 2 15 | print(array) 16 | print(np.nonzero(array)) -------------------------------------------------------------------------------- /tests/2d/numpy/nonzero.py.exp: -------------------------------------------------------------------------------- 1 | array([[0.0, 1.0, 2.0, 3.0], 2 | [4.0, 5.0, 6.0, 7.0], 3 | [8.0, 9.0, 10.0, 11.0], 4 | [12.0, 13.0, 14.0, 15.0]], dtype=float64) 5 | array([[True, True, True, True], 6 | [True, False, False, False], 7 | [False, False, False, False], 8 | [False, False, False, False]], dtype=bool) 9 | (array([0, 0, 0, 0, 1], dtype=uint16), array([0, 1, 2, 3, 0], dtype=uint16)) 10 | array([[0, 3], 11 | [6, 9]], dtype=uint8) 12 | (array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16)) 13 | array([[0, 3], 14 | [6, 9]], dtype=int8) 15 | (array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16)) 16 | array([[0, 3], 17 | [6, 9]], dtype=uint16) 18 | (array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16)) 19 | array([[0, 3], 20 | [6, 9]], dtype=int16) 21 | (array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16)) 22 | array([[0.0, 3.0], 23 | [6.0, 9.0]], dtype=float64) 24 | (array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16)) 25 | -------------------------------------------------------------------------------- /tests/2d/numpy/ones.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | print(np.ones(3)) 9 | print(np.ones((3,3))) 10 | 11 | for dtype in dtypes: 12 | print(np.ones((3,3), dtype=dtype)) 13 | print(np.ones((4,2), dtype=dtype)) 14 | -------------------------------------------------------------------------------- /tests/2d/numpy/ones.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0, 1.0, 1.0], dtype=float64) 2 | array([[1.0, 1.0, 1.0], 3 | [1.0, 1.0, 1.0], 4 | [1.0, 1.0, 1.0]], dtype=float64) 5 | array([[1, 1, 1], 6 | [1, 1, 1], 7 | [1, 1, 1]], dtype=uint8) 8 | array([[1, 1], 9 | [1, 1], 10 | [1, 1], 11 | [1, 1]], dtype=uint8) 12 | array([[1, 1, 1], 13 | [1, 1, 1], 14 | [1, 1, 1]], dtype=int8) 15 | array([[1, 1], 16 | [1, 1], 17 | [1, 1], 18 | [1, 1]], dtype=int8) 19 | array([[1, 1, 1], 20 | [1, 1, 1], 21 | [1, 1, 1]], dtype=uint16) 22 | array([[1, 1], 23 | [1, 1], 24 | [1, 1], 25 | [1, 1]], dtype=uint16) 26 | array([[1, 1, 1], 27 | [1, 1, 1], 28 | [1, 1, 1]], dtype=int16) 29 | array([[1, 1], 30 | [1, 1], 31 | [1, 1], 32 | [1, 1]], dtype=int16) 33 | array([[1.0, 1.0, 1.0], 34 | [1.0, 1.0, 1.0], 35 | [1.0, 1.0, 1.0]], dtype=float64) 36 | array([[1.0, 1.0], 37 | [1.0, 1.0], 38 | [1.0, 1.0], 39 | [1.0, 1.0]], dtype=float64) 40 | -------------------------------------------------------------------------------- /tests/2d/numpy/or.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 7 | 8 | for dtype_a in dtypes: 9 | a = np.array(range(5), dtype=dtype_a) 10 | for dtype_b in dtypes: 11 | b = np.array(range(250, 255), dtype=dtype_b) 12 | try: 13 | print('a | b: ', a | b) 14 | except Exception as e: 15 | print(e) 16 | 17 | b = np.array([False, True, False, True, False], dtype=np.bool) 18 | try: 19 | print('a | b (bool): ', a | b) 20 | except Exception as e: 21 | print(e) -------------------------------------------------------------------------------- /tests/2d/numpy/or.py.exp: -------------------------------------------------------------------------------- 1 | a | b: array([250, 251, 254, 255, 254], dtype=uint8) 2 | a | b: array([-6, -5, -2, -1, -2], dtype=int16) 3 | a | b: array([250, 251, 254, 255, 254], dtype=uint16) 4 | a | b: array([250, 251, 254, 255, 254], dtype=int16) 5 | a | b (bool): array([0, 1, 2, 3, 4], dtype=uint8) 6 | a | b: array([250, 251, 254, 255, 254], dtype=int16) 7 | a | b: array([-6, -5, -2, -1, -2], dtype=int8) 8 | a | b: array([250, 251, 254, 255, 254], dtype=int16) 9 | a | b: array([250, 251, 254, 255, 254], dtype=int16) 10 | a | b (bool): array([0, 1, 2, 3, 4], dtype=int16) 11 | a | b: array([250, 251, 254, 255, 254], dtype=uint16) 12 | a | b: array([-6, -5, -2, -1, -2], dtype=int16) 13 | a | b: array([250, 251, 254, 255, 254], dtype=uint16) 14 | dtype of int32 is not supported 15 | a | b (bool): array([0, 1, 2, 3, 4], dtype=uint16) 16 | a | b: array([250, 251, 254, 255, 254], dtype=int16) 17 | a | b: array([-6, -5, -2, -1, -2], dtype=int16) 18 | dtype of int32 is not supported 19 | a | b: array([250, 251, 254, 255, 254], dtype=int16) 20 | a | b (bool): array([0, 1, 2, 3, 4], dtype=int16) 21 | -------------------------------------------------------------------------------- /tests/2d/numpy/random.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | rng = np.random.Generator(1234) 7 | 8 | for generator in (rng.normal, rng.random, rng.uniform): 9 | random_array = generator(size=(1, 2)) 10 | print("array shape:", random_array.shape) -------------------------------------------------------------------------------- /tests/2d/numpy/random.py.exp: -------------------------------------------------------------------------------- 1 | array shape: (1, 2) 2 | array shape: (1, 2) 3 | array shape: (1, 2) 4 | -------------------------------------------------------------------------------- /tests/2d/numpy/reshape.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype in dtypes: 9 | print() 10 | print('=' * 50) 11 | a = np.array(range(12), dtype=dtype).reshape((3, 4)) 12 | print(a) 13 | b = a[0,:] 14 | print(b.reshape((1,4))) 15 | b = a[:,0] 16 | print(b.reshape((1,3))) 17 | 18 | -------------------------------------------------------------------------------- /tests/2d/numpy/reshape.py.exp: -------------------------------------------------------------------------------- 1 | 2 | ================================================== 3 | array([[0, 1, 2, 3], 4 | [4, 5, 6, 7], 5 | [8, 9, 10, 11]], dtype=uint8) 6 | array([[0, 1, 2, 3]], dtype=uint8) 7 | array([[0, 4, 8]], dtype=uint8) 8 | 9 | ================================================== 10 | array([[0, 1, 2, 3], 11 | [4, 5, 6, 7], 12 | [8, 9, 10, 11]], dtype=int8) 13 | array([[0, 1, 2, 3]], dtype=int8) 14 | array([[0, 4, 8]], dtype=int8) 15 | 16 | ================================================== 17 | array([[0, 1, 2, 3], 18 | [4, 5, 6, 7], 19 | [8, 9, 10, 11]], dtype=uint16) 20 | array([[0, 1, 2, 3]], dtype=uint16) 21 | array([[0, 4, 8]], dtype=uint16) 22 | 23 | ================================================== 24 | array([[0, 1, 2, 3], 25 | [4, 5, 6, 7], 26 | [8, 9, 10, 11]], dtype=int16) 27 | array([[0, 1, 2, 3]], dtype=int16) 28 | array([[0, 4, 8]], dtype=int16) 29 | 30 | ================================================== 31 | array([[0.0, 1.0, 2.0, 3.0], 32 | [4.0, 5.0, 6.0, 7.0], 33 | [8.0, 9.0, 10.0, 11.0]], dtype=float64) 34 | array([[0.0, 1.0, 2.0, 3.0]], dtype=float64) 35 | array([[0.0, 4.0, 8.0]], dtype=float64) 36 | -------------------------------------------------------------------------------- /tests/2d/numpy/right_shift.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | np.set_printoptions(threshold=100) 6 | 7 | 8 | shift_values = ( 9 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 10 | (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 11 | (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), 12 | ) 13 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 14 | 15 | 16 | for shift_value in shift_values: 17 | for dtype1 in dtypes: 18 | x1 = np.array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=dtype1) 19 | for dtype2 in dtypes: 20 | x2 = np.array(shift_value, dtype=dtype2) 21 | print(np.right_shift(x1, x2)) 22 | -------------------------------------------------------------------------------- /tests/2d/numpy/right_shift.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint8) 2 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 3 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16) 4 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 5 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 6 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int8) 7 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16) 8 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 9 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16) 10 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int8) 11 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16) 12 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 13 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 14 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 15 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 16 | array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16) 17 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint8) 18 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 19 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint16) 20 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 21 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 22 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int8) 23 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint16) 24 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 25 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint16) 26 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int8) 27 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint16) 28 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 29 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 30 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 31 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 32 | array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16) 33 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint8) 34 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 35 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint16) 36 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 37 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 38 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int8) 39 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint16) 40 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 41 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint16) 42 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int8) 43 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint16) 44 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 45 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 46 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 47 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 48 | array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16) 49 | -------------------------------------------------------------------------------- /tests/2d/numpy/savetxt.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | a = np.array(range(9)) 7 | 8 | print('savetxt with linear arrays') 9 | np.savetxt('savetxt.dat', a) 10 | 11 | with open('savetxt.dat', 'r') as fin: 12 | print(fin.read()) 13 | 14 | a = a.reshape((3, 3)) 15 | 16 | print('savetxt with no keyword arguments') 17 | np.savetxt('savetxt.dat', a) 18 | 19 | with open('savetxt.dat', 'r') as fin: 20 | print(fin.read()) 21 | 22 | print('savetxt with delimiter') 23 | np.savetxt('savetxt.dat', a, delimiter=',') 24 | 25 | with open('savetxt.dat', 'r') as fin: 26 | print(fin.read()) 27 | 28 | print('savetxt with header') 29 | np.savetxt('savetxt.dat', a, header='column1 column2 column3') 30 | 31 | with open('savetxt.dat', 'r') as fin: 32 | print(fin.read()) 33 | 34 | print('savetxt with footer') 35 | np.savetxt('savetxt.dat', a, footer='written data file') 36 | 37 | with open('savetxt.dat', 'r') as fin: 38 | print(fin.read()) -------------------------------------------------------------------------------- /tests/2d/numpy/savetxt.py.exp: -------------------------------------------------------------------------------- 1 | savetxt with linear arrays 2 | 0.000000000000000 3 | 1.000000000000000 4 | 2.000000000000000 5 | 3.000000000000000 6 | 4.000000000000000 7 | 5.000000000000000 8 | 6.000000000000000 9 | 7.000000000000000 10 | 8.000000000000000 11 | 12 | savetxt with no keyword arguments 13 | 0.000000000000000 1.000000000000000 2.000000000000000 14 | 3.000000000000000 4.000000000000000 5.000000000000000 15 | 6.000000000000000 7.000000000000000 8.000000000000000 16 | 17 | savetxt with delimiter 18 | 0.000000000000000,1.000000000000000,2.000000000000000 19 | 3.000000000000000,4.000000000000000,5.000000000000000 20 | 6.000000000000000,7.000000000000000,8.000000000000000 21 | 22 | savetxt with header 23 | # column1 column2 column3 24 | 0.000000000000000 1.000000000000000 2.000000000000000 25 | 3.000000000000000 4.000000000000000 5.000000000000000 26 | 6.000000000000000 7.000000000000000 8.000000000000000 27 | 28 | savetxt with footer 29 | 0.000000000000000 1.000000000000000 2.000000000000000 30 | 3.000000000000000 4.000000000000000 5.000000000000000 31 | 6.000000000000000 7.000000000000000 8.000000000000000 32 | # written data file 33 | 34 | -------------------------------------------------------------------------------- /tests/2d/numpy/signal.py: -------------------------------------------------------------------------------- 1 | import math 2 | try: 3 | from ulab import numpy as np 4 | from ulab import scipy as spy 5 | except ImportError: 6 | import numpy as np 7 | import scipy as spy 8 | 9 | x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float) 10 | sos = np.array([[1, 2, 3, 1, 5, 6], [1, 2, 3, 1, 5, 6]],dtype=np.float) 11 | result = spy.signal.sosfilt(sos, x) 12 | 13 | ref_result = np.array([0.0000e+00, 1.0000e+00, -4.0000e+00, 2.4000e+01, -1.0400e+02, 4.4000e+02, -1.7280e+03, 6.5320e+03, -2.3848e+04, 8.4864e+04], dtype=np.float) 14 | cmp_result = [] 15 | for p,q in zip(list(result), list(ref_result)): 16 | cmp_result.append(math.isclose(p, q, rel_tol=1e-06, abs_tol=1e-06)) 17 | print(cmp_result) 18 | 19 | x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 20 | sos = np.array([[1, 2, 3, 1, 5, 6], [1, 2, 3, 1, 5, 6]],dtype=np.float) 21 | zi = np.array([[1, 2], [3, 4]],dtype=np.float) 22 | y, zo = spy.signal.sosfilt(sos, x, zi=zi) 23 | 24 | y_ref = np.array([ 4.00000e+00, -1.60000e+01, 6.30000e+01, -2.27000e+02, 8.03000e+02, -2.75100e+03, 9.27100e+03, -3.07750e+04, 1.01067e+05, -3.28991e+05], dtype=np.float) 25 | zo_ref = np.array([[37242.0, 74835.],[1026187.0, 1936542.0]], dtype=np.float) 26 | cmp_result = [] 27 | for p,q in zip(list(y), list(y_ref)): 28 | cmp_result.append(math.isclose(p, q, rel_tol=1e-06, abs_tol=1e-06)) 29 | print(cmp_result) 30 | 31 | cmp_result = [] 32 | for i in range(2): 33 | temp = [] 34 | for j in range(2): 35 | temp.append(math.isclose(zo[i][j], zo_ref[i][j], rel_tol=1E-9, abs_tol=1E-9)) 36 | cmp_result.append(temp) 37 | print(cmp_result) 38 | -------------------------------------------------------------------------------- /tests/2d/numpy/signal.py.exp: -------------------------------------------------------------------------------- 1 | [True, True, True, True, True, True, True, True, True, True] 2 | [True, True, True, True, True, True, True, True, True, True] 3 | [[True, True], [True, True]] 4 | -------------------------------------------------------------------------------- /tests/2d/numpy/size.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | a = np.zeros((3, 4)) 7 | 8 | print(np.size(a, axis=0)) 9 | print(np.size(a, axis=1)) 10 | print(np.size(a, axis=None)) 11 | -------------------------------------------------------------------------------- /tests/2d/numpy/size.py.exp: -------------------------------------------------------------------------------- 1 | 3 2 | 4 3 | 12 4 | -------------------------------------------------------------------------------- /tests/2d/numpy/sort.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | for dtype in dtypes: 9 | print() 10 | a = np.array([], dtype=dtype) 11 | print(np.sort(a, axis=0)) 12 | print(list(np.argsort(a, axis=0))) 13 | 14 | a = np.array([4, 1, 3, 2], dtype=dtype) 15 | print(np.sort(a, axis=0)) 16 | print(list(np.argsort(a, axis=0))) 17 | 18 | 19 | -------------------------------------------------------------------------------- /tests/2d/numpy/sort.py.exp: -------------------------------------------------------------------------------- 1 | 2 | array([], dtype=uint8) 3 | [] 4 | array([1, 2, 3, 4], dtype=uint8) 5 | [1, 3, 2, 0] 6 | 7 | array([], dtype=int8) 8 | [] 9 | array([1, 2, 3, 4], dtype=int8) 10 | [1, 3, 2, 0] 11 | 12 | array([], dtype=uint16) 13 | [] 14 | array([1, 2, 3, 4], dtype=uint16) 15 | [1, 3, 2, 0] 16 | 17 | array([], dtype=int16) 18 | [] 19 | array([1, 2, 3, 4], dtype=int16) 20 | [1, 3, 2, 0] 21 | 22 | array([], dtype=float64) 23 | [] 24 | array([1.0, 2.0, 3.0, 4.0], dtype=float64) 25 | [1, 3, 2, 0] 26 | -------------------------------------------------------------------------------- /tests/2d/numpy/sum.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | for dtype in (np.uint8, np.int8, np.uint16, np.int8, np.float): 7 | a = np.array(range(12), dtype=dtype) 8 | b = a.reshape((3, 4)) 9 | 10 | print(a) 11 | print(b) 12 | print() 13 | 14 | print(np.sum(a)) 15 | print(np.sum(a, axis=0)) 16 | print(np.sum(a, axis=0, keepdims=True)) 17 | 18 | print() 19 | print(np.sum(b)) 20 | print(np.sum(b, axis=0)) 21 | print(np.sum(b, axis=1)) 22 | print(np.sum(b, axis=0, keepdims=True)) 23 | print(np.sum(b, axis=1, keepdims=True)) -------------------------------------------------------------------------------- /tests/2d/numpy/sum.py.exp: -------------------------------------------------------------------------------- 1 | array([0, 1, 2, ..., 9, 10, 11], dtype=uint8) 2 | array([[0, 1, 2, 3], 3 | [4, 5, 6, 7], 4 | [8, 9, 10, 11]], dtype=uint8) 5 | 6 | 66 7 | 66 8 | array([66], dtype=uint8) 9 | 10 | 66 11 | array([12, 15, 18, 21], dtype=uint8) 12 | array([6, 22, 38], dtype=uint8) 13 | array([[12, 15, 18, 21]], dtype=uint8) 14 | array([[6], 15 | [22], 16 | [38]], dtype=uint8) 17 | array([0, 1, 2, ..., 9, 10, 11], dtype=int8) 18 | array([[0, 1, 2, 3], 19 | [4, 5, 6, 7], 20 | [8, 9, 10, 11]], dtype=int8) 21 | 22 | 66 23 | 66 24 | array([66], dtype=int8) 25 | 26 | 66 27 | array([12, 15, 18, 21], dtype=int8) 28 | array([6, 22, 38], dtype=int8) 29 | array([[12, 15, 18, 21]], dtype=int8) 30 | array([[6], 31 | [22], 32 | [38]], dtype=int8) 33 | array([0, 1, 2, ..., 9, 10, 11], dtype=uint16) 34 | array([[0, 1, 2, 3], 35 | [4, 5, 6, 7], 36 | [8, 9, 10, 11]], dtype=uint16) 37 | 38 | 66 39 | 66 40 | array([66], dtype=uint16) 41 | 42 | 66 43 | array([12, 15, 18, 21], dtype=uint16) 44 | array([6, 22, 38], dtype=uint16) 45 | array([[12, 15, 18, 21]], dtype=uint16) 46 | array([[6], 47 | [22], 48 | [38]], dtype=uint16) 49 | array([0, 1, 2, ..., 9, 10, 11], dtype=int8) 50 | array([[0, 1, 2, 3], 51 | [4, 5, 6, 7], 52 | [8, 9, 10, 11]], dtype=int8) 53 | 54 | 66 55 | 66 56 | array([66], dtype=int8) 57 | 58 | 66 59 | array([12, 15, 18, 21], dtype=int8) 60 | array([6, 22, 38], dtype=int8) 61 | array([[12, 15, 18, 21]], dtype=int8) 62 | array([[6], 63 | [22], 64 | [38]], dtype=int8) 65 | array([0.0, 1.0, 2.0, ..., 9.0, 10.0, 11.0], dtype=float64) 66 | array([[0.0, 1.0, 2.0, 3.0], 67 | [4.0, 5.0, 6.0, 7.0], 68 | [8.0, 9.0, 10.0, 11.0]], dtype=float64) 69 | 70 | 66.0 71 | 66.0 72 | array([66.0], dtype=float64) 73 | 74 | 66.0 75 | array([12.0, 15.0, 18.0, 21.0], dtype=float64) 76 | array([6.0, 22.0, 38.0], dtype=float64) 77 | array([[12.0, 15.0, 18.0, 21.0]], dtype=float64) 78 | array([[6.0], 79 | [22.0], 80 | [38.0]], dtype=float64) 81 | -------------------------------------------------------------------------------- /tests/2d/numpy/take.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | print('flattened array') 9 | for dtype in dtypes: 10 | a = np.array(range(12), dtype=dtype).reshape((3, 4)) 11 | print(np.take(a, (0, 10))) 12 | 13 | print('\n1D arrays') 14 | for dtype in dtypes: 15 | a = np.array(range(12), dtype=dtype) 16 | print('\na:', a) 17 | indices = (0, 2, 2, 1) 18 | print(np.take(a, indices)) 19 | indices = np.array([0, 2, 2, 1], dtype=np.uint8) 20 | print(np.take(a, indices)) 21 | 22 | print('\n2D arrays') 23 | for dtype in dtypes: 24 | a = np.array(range(12), dtype=dtype).reshape((3, 4)) 25 | print('\na:', a) 26 | print('\nfirst axis') 27 | print(np.take(a, (0, 2, 2, 1), axis=0)) 28 | print('\nsecond axis') 29 | print(np.take(a, (0, 2, 2, 1), axis=1)) 30 | 31 | -------------------------------------------------------------------------------- /tests/2d/numpy/take.py.exp: -------------------------------------------------------------------------------- 1 | flattened array 2 | array([0, 10], dtype=uint8) 3 | array([0, 10], dtype=int8) 4 | array([0, 10], dtype=uint16) 5 | array([0, 10], dtype=int16) 6 | array([0.0, 10.0], dtype=float64) 7 | 8 | 1D arrays 9 | 10 | a: array([0, 1, 2, ..., 9, 10, 11], dtype=uint8) 11 | array([0, 2, 2, 1], dtype=uint8) 12 | array([0, 2, 2, 1], dtype=uint8) 13 | 14 | a: array([0, 1, 2, ..., 9, 10, 11], dtype=int8) 15 | array([0, 2, 2, 1], dtype=int8) 16 | array([0, 2, 2, 1], dtype=int8) 17 | 18 | a: array([0, 1, 2, ..., 9, 10, 11], dtype=uint16) 19 | array([0, 2, 2, 1], dtype=uint16) 20 | array([0, 2, 2, 1], dtype=uint16) 21 | 22 | a: array([0, 1, 2, ..., 9, 10, 11], dtype=int16) 23 | array([0, 2, 2, 1], dtype=int16) 24 | array([0, 2, 2, 1], dtype=int16) 25 | 26 | a: array([0.0, 1.0, 2.0, ..., 9.0, 10.0, 11.0], dtype=float64) 27 | array([0.0, 2.0, 2.0, 1.0], dtype=float64) 28 | array([0.0, 2.0, 2.0, 1.0], dtype=float64) 29 | 30 | 2D arrays 31 | 32 | a: array([[0, 1, 2, 3], 33 | [4, 5, 6, 7], 34 | [8, 9, 10, 11]], dtype=uint8) 35 | 36 | first axis 37 | array([[0, 1, 2, 3], 38 | [8, 9, 10, 11], 39 | [8, 9, 10, 11], 40 | [4, 5, 6, 7]], dtype=uint8) 41 | 42 | second axis 43 | array([[0, 2, 2, 1], 44 | [4, 6, 6, 5], 45 | [8, 10, 10, 9]], dtype=uint8) 46 | 47 | a: array([[0, 1, 2, 3], 48 | [4, 5, 6, 7], 49 | [8, 9, 10, 11]], dtype=int8) 50 | 51 | first axis 52 | array([[0, 1, 2, 3], 53 | [8, 9, 10, 11], 54 | [8, 9, 10, 11], 55 | [4, 5, 6, 7]], dtype=int8) 56 | 57 | second axis 58 | array([[0, 2, 2, 1], 59 | [4, 6, 6, 5], 60 | [8, 10, 10, 9]], dtype=int8) 61 | 62 | a: array([[0, 1, 2, 3], 63 | [4, 5, 6, 7], 64 | [8, 9, 10, 11]], dtype=uint16) 65 | 66 | first axis 67 | array([[0, 1, 2, 3], 68 | [8, 9, 10, 11], 69 | [8, 9, 10, 11], 70 | [4, 5, 6, 7]], dtype=uint16) 71 | 72 | second axis 73 | array([[0, 2, 2, 1], 74 | [4, 6, 6, 5], 75 | [8, 10, 10, 9]], dtype=uint16) 76 | 77 | a: array([[0, 1, 2, 3], 78 | [4, 5, 6, 7], 79 | [8, 9, 10, 11]], dtype=int16) 80 | 81 | first axis 82 | array([[0, 1, 2, 3], 83 | [8, 9, 10, 11], 84 | [8, 9, 10, 11], 85 | [4, 5, 6, 7]], dtype=int16) 86 | 87 | second axis 88 | array([[0, 2, 2, 1], 89 | [4, 6, 6, 5], 90 | [8, 10, 10, 9]], dtype=int16) 91 | 92 | a: array([[0.0, 1.0, 2.0, 3.0], 93 | [4.0, 5.0, 6.0, 7.0], 94 | [8.0, 9.0, 10.0, 11.0]], dtype=float64) 95 | 96 | first axis 97 | array([[0.0, 1.0, 2.0, 3.0], 98 | [8.0, 9.0, 10.0, 11.0], 99 | [8.0, 9.0, 10.0, 11.0], 100 | [4.0, 5.0, 6.0, 7.0]], dtype=float64) 101 | 102 | second axis 103 | array([[0.0, 2.0, 2.0, 1.0], 104 | [4.0, 6.0, 6.0, 5.0], 105 | [8.0, 10.0, 10.0, 9.0]], dtype=float64) 106 | -------------------------------------------------------------------------------- /tests/2d/numpy/vectorize.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | 7 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 8 | 9 | square = np.vectorize(lambda n: n*n) 10 | 11 | for dtype in dtypes: 12 | a = np.array(range(9), dtype=dtype).reshape((3, 3)) 13 | print(a) 14 | print(square(a)) 15 | 16 | b = a[:,2] 17 | print(square(b)) 18 | print() 19 | -------------------------------------------------------------------------------- /tests/2d/numpy/vectorize.py.exp: -------------------------------------------------------------------------------- 1 | array([[0, 1, 2], 2 | [3, 4, 5], 3 | [6, 7, 8]], dtype=uint8) 4 | array([[0.0, 1.0, 4.0], 5 | [9.0, 16.0, 25.0], 6 | [36.0, 49.0, 64.0]], dtype=float64) 7 | array([4.0, 25.0, 64.0], dtype=float64) 8 | 9 | array([[0, 1, 2], 10 | [3, 4, 5], 11 | [6, 7, 8]], dtype=int8) 12 | array([[0.0, 1.0, 4.0], 13 | [9.0, 16.0, 25.0], 14 | [36.0, 49.0, 64.0]], dtype=float64) 15 | array([4.0, 25.0, 64.0], dtype=float64) 16 | 17 | array([[0, 1, 2], 18 | [3, 4, 5], 19 | [6, 7, 8]], dtype=uint16) 20 | array([[0.0, 1.0, 4.0], 21 | [9.0, 16.0, 25.0], 22 | [36.0, 49.0, 64.0]], dtype=float64) 23 | array([4.0, 25.0, 64.0], dtype=float64) 24 | 25 | array([[0, 1, 2], 26 | [3, 4, 5], 27 | [6, 7, 8]], dtype=int16) 28 | array([[0.0, 1.0, 4.0], 29 | [9.0, 16.0, 25.0], 30 | [36.0, 49.0, 64.0]], dtype=float64) 31 | array([4.0, 25.0, 64.0], dtype=float64) 32 | 33 | array([[0.0, 1.0, 2.0], 34 | [3.0, 4.0, 5.0], 35 | [6.0, 7.0, 8.0]], dtype=float64) 36 | array([[0.0, 1.0, 4.0], 37 | [9.0, 16.0, 25.0], 38 | [36.0, 49.0, 64.0]], dtype=float64) 39 | array([4.0, 25.0, 64.0], dtype=float64) 40 | 41 | -------------------------------------------------------------------------------- /tests/2d/numpy/where.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | 3 | 4 | a = np.array(range(8)) 5 | 6 | print(np.where(a < 4, 1, 0)) 7 | print(np.where(a < 4, 2 * a, 0)) 8 | 9 | a = np.array(range(12)).reshape((3, 4)) 10 | print(np.where(a < 6, a, -1)) 11 | 12 | b = np.array(range(4)) 13 | print(np.where(a < 6, 10 + b, -1)) 14 | 15 | # test upcasting here 16 | b = np.array(range(4), dtype=np.uint8) 17 | c = np.array([25, 25, 25, 25], dtype=np.int16) 18 | print(np.where(a < 6, b, c)) 19 | -------------------------------------------------------------------------------- /tests/2d/numpy/where.py.exp: -------------------------------------------------------------------------------- 1 | array([1, 1, 1, 1, 0, 0, 0, 0], dtype=uint8) 2 | array([0.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0, 0.0], dtype=float64) 3 | array([[0.0, 1.0, 2.0, 3.0], 4 | [4.0, 5.0, -1.0, -1.0], 5 | [-1.0, -1.0, -1.0, -1.0]], dtype=float64) 6 | array([[10.0, 11.0, 12.0, 13.0], 7 | [10.0, 11.0, -1.0, -1.0], 8 | [-1.0, -1.0, -1.0, -1.0]], dtype=float64) 9 | array([[0, 1, 2, 3], 10 | [0, 1, 25, 25], 11 | [25, 25, 25, 25]], dtype=int16) 12 | -------------------------------------------------------------------------------- /tests/2d/numpy/xor.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except ImportError: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16) 7 | 8 | for dtype_a in dtypes: 9 | a = np.array(range(5), dtype=dtype_a) 10 | for dtype_b in dtypes: 11 | b = np.array(range(250, 255), dtype=dtype_b) 12 | try: 13 | print('a ^ b: ', a ^ b) 14 | except Exception as e: 15 | print(e) 16 | 17 | b = np.array([False, True, False, True, False], dtype=np.bool) 18 | try: 19 | print('a ^ b (bool): ', a ^ b) 20 | except Exception as e: 21 | print(e) -------------------------------------------------------------------------------- /tests/2d/numpy/xor.py.exp: -------------------------------------------------------------------------------- 1 | a ^ b: array([250, 250, 254, 254, 250], dtype=uint8) 2 | a ^ b: array([-6, -6, -2, -2, -6], dtype=int16) 3 | a ^ b: array([250, 250, 254, 254, 250], dtype=uint16) 4 | a ^ b: array([250, 250, 254, 254, 250], dtype=int16) 5 | a ^ b (bool): array([0, 0, 2, 2, 4], dtype=uint8) 6 | a ^ b: array([250, 250, 254, 254, 250], dtype=int16) 7 | a ^ b: array([-6, -6, -2, -2, -6], dtype=int8) 8 | a ^ b: array([250, 250, 254, 254, 250], dtype=int16) 9 | a ^ b: array([250, 250, 254, 254, 250], dtype=int16) 10 | a ^ b (bool): array([0, 0, 2, 2, 4], dtype=int16) 11 | a ^ b: array([250, 250, 254, 254, 250], dtype=uint16) 12 | a ^ b: array([-6, -6, -2, -2, -6], dtype=int16) 13 | a ^ b: array([250, 250, 254, 254, 250], dtype=uint16) 14 | dtype of int32 is not supported 15 | a ^ b (bool): array([0, 0, 2, 2, 4], dtype=uint16) 16 | a ^ b: array([250, 250, 254, 254, 250], dtype=int16) 17 | a ^ b: array([-6, -6, -2, -2, -6], dtype=int16) 18 | dtype of int32 is not supported 19 | a ^ b: array([250, 250, 254, 254, 250], dtype=int16) 20 | a ^ b (bool): array([0, 0, 2, 2, 4], dtype=int16) 21 | -------------------------------------------------------------------------------- /tests/2d/numpy/zeros.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | except: 4 | import numpy as np 5 | 6 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float) 7 | 8 | print(np.zeros(3)) 9 | print(np.zeros((3,3))) 10 | 11 | for dtype in dtypes: 12 | print(np.zeros((3,3), dtype=dtype)) 13 | print(np.zeros((4,2), dtype=dtype)) 14 | 15 | try: 16 | np.zeros((1<<31, 1<<31)) 17 | except ValueError: 18 | print("ValueError") 19 | 20 | try: 21 | np.zeros((2147483653, 2147483649)) 22 | except ValueError: 23 | print("ValueError") 24 | 25 | try: 26 | np.zeros((194899806, 189294637612)) 27 | except ValueError: 28 | print("ValueError") 29 | -------------------------------------------------------------------------------- /tests/2d/numpy/zeros.py.exp: -------------------------------------------------------------------------------- 1 | array([0.0, 0.0, 0.0], dtype=float64) 2 | array([[0.0, 0.0, 0.0], 3 | [0.0, 0.0, 0.0], 4 | [0.0, 0.0, 0.0]], dtype=float64) 5 | array([[0, 0, 0], 6 | [0, 0, 0], 7 | [0, 0, 0]], dtype=uint8) 8 | array([[0, 0], 9 | [0, 0], 10 | [0, 0], 11 | [0, 0]], dtype=uint8) 12 | array([[0, 0, 0], 13 | [0, 0, 0], 14 | [0, 0, 0]], dtype=int8) 15 | array([[0, 0], 16 | [0, 0], 17 | [0, 0], 18 | [0, 0]], dtype=int8) 19 | array([[0, 0, 0], 20 | [0, 0, 0], 21 | [0, 0, 0]], dtype=uint16) 22 | array([[0, 0], 23 | [0, 0], 24 | [0, 0], 25 | [0, 0]], dtype=uint16) 26 | array([[0, 0, 0], 27 | [0, 0, 0], 28 | [0, 0, 0]], dtype=int16) 29 | array([[0, 0], 30 | [0, 0], 31 | [0, 0], 32 | [0, 0]], dtype=int16) 33 | array([[0.0, 0.0, 0.0], 34 | [0.0, 0.0, 0.0], 35 | [0.0, 0.0, 0.0]], dtype=float64) 36 | array([[0.0, 0.0], 37 | [0.0, 0.0], 38 | [0.0, 0.0], 39 | [0.0, 0.0]], dtype=float64) 40 | ValueError 41 | ValueError 42 | ValueError 43 | -------------------------------------------------------------------------------- /tests/2d/scipy/cho_solve.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | try: 4 | from ulab import scipy, numpy as np 5 | except ImportError: 6 | import scipy 7 | import numpy as np 8 | 9 | ## test cholesky solve 10 | L = np.array([[3, 0, 0, 0], [2, 1, 0, 0], [1, 0, 1, 0], [1, 2, 1, 8]]) 11 | b = np.array([4, 2, 4, 2]) 12 | 13 | # L needs to be a lower triangular matrix 14 | result = scipy.linalg.cho_solve(L, b) 15 | ref_result = np.array([-0.01388888888888906, -0.6458333333333331, 2.677083333333333, -0.01041666666666667]) 16 | 17 | for i in range(4): 18 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-6, abs_tol=1E-6)) 19 | 20 | ## test cholesky and cho_solve together 21 | C = np.array([[18, 22, 54, 42], [22, 70, 86, 62], [54, 86, 174, 134], [42, 62, 134, 106]]) 22 | L = np.linalg.cholesky(C) 23 | 24 | # L is a lower triangular matrix obtained by performing cholesky of positive-definite linear system 25 | result = scipy.linalg.cho_solve(L, b) 26 | ref_result = np.array([6.5625, 1.1875, -2.9375, 0.4375]) 27 | 28 | for i in range(4): 29 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-6, abs_tol=1E-6)) 30 | -------------------------------------------------------------------------------- /tests/2d/scipy/cho_solve.py.exp: -------------------------------------------------------------------------------- 1 | True 2 | True 3 | True 4 | True 5 | True 6 | True 7 | True 8 | True 9 | -------------------------------------------------------------------------------- /tests/2d/scipy/integrate.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from math import * 3 | 4 | try: 5 | from ulab import scipy 6 | except ImportError: 7 | import scipy 8 | 9 | f = lambda x: x * sin(x) * exp(x) 10 | a=1 11 | b=2 12 | 13 | (res, err) = scipy.integrate.tanhsinh(f, a, b) 14 | if isclose (res, 7.11263821415851) and isclose (err, 5.438231077315757e-14): 15 | print (res, err) 16 | 17 | res = scipy.integrate.romberg(f, a, b) 18 | if isclose (res, 7.112638214158507): 19 | print (res) 20 | 21 | res = scipy.integrate.simpson(f, a, b) 22 | if isclose (res, 7.112638214158494): 23 | print (res) 24 | 25 | (res, err) = scipy.integrate.quad(f, a, b) 26 | if isclose (res, 7.112638214158507) and isclose (err, 7.686723611780195e-14): 27 | print (res, err) 28 | 29 | -------------------------------------------------------------------------------- /tests/2d/scipy/integrate.py.exp: -------------------------------------------------------------------------------- 1 | 7.11263821415851 5.438231077315757e-14 2 | 7.112638214158507 3 | 7.112638214158494 4 | 7.112638214158507 7.686723611780195e-14 5 | -------------------------------------------------------------------------------- /tests/2d/scipy/solve_triangular.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | try: 4 | from ulab import scipy, numpy as np 5 | except ImportError: 6 | import scipy 7 | import numpy as np 8 | 9 | A = np.array([[3, 0, 2, 6], [2, 1, 0, 1], [1, 0, 1, 4], [1, 2, 1, 8]]) 10 | b = np.array([4, 2, 4, 2]) 11 | 12 | # forward substitution 13 | result = scipy.linalg.solve_triangular(A, b, lower=True) 14 | ref_result = np.array([1.333333333, -0.666666666, 2.666666666, -0.083333333]) 15 | for i in range(4): 16 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-6, abs_tol=1E-6)) 17 | 18 | # backward substitution 19 | result = scipy.linalg.solve_triangular(A, b, lower=False) 20 | ref_result = np.array([-1.166666666, 1.75, 3.0, 0.25]) 21 | for i in range(4): 22 | print(math.isclose(result[i], ref_result[i], rel_tol=1E-6, abs_tol=1E-6)) 23 | -------------------------------------------------------------------------------- /tests/2d/scipy/solve_triangular.py.exp: -------------------------------------------------------------------------------- 1 | True 2 | True 3 | True 4 | True 5 | True 6 | True 7 | True 8 | True 9 | -------------------------------------------------------------------------------- /tests/2d/scipy/sosfilt.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ulab import numpy as np 3 | from ulab import scipy as spy 4 | except: 5 | import numpy as np 6 | import scipy as spy 7 | 8 | x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 9 | sos = [[1, 2, 3, 1, 5, 6], [1, 2, 3, 1, 5, 6], [1, 2, 3, 1, 5, 6]] 10 | zi = np.array([[1, 2], [3, 4], [5, 6]],dtype=np.float) 11 | y, zo = spy.signal.sosfilt(sos, x, zi=zi) 12 | print('y: ', y) 13 | print('zo: ', zo) 14 | -------------------------------------------------------------------------------- /tests/2d/scipy/sosfilt.py.exp: -------------------------------------------------------------------------------- 1 | y: array([9.0, -47.0, 224.0, -987.0, 4129.0, -16549.0, 64149.0, -241937.0, 892121.0, -3228165.0], dtype=float64) 2 | zo: array([[37242.0, 74835.0], 3 | [1026187.0, 1936542.0], 4 | [10433318.0, 18382017.0]], dtype=float64) 5 | -------------------------------------------------------------------------------- /tests/2d/utils/from_buffer.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | from ulab import utils 3 | 4 | a = bytearray([1, 0, 0, 1, 0, 255, 255, 255]) 5 | print(utils.from_uint16_buffer(a)) 6 | a = bytearray([1, 0, 0, 1, 0, 255, 255, 255]) 7 | print(utils.from_int16_buffer(a)) 8 | 9 | a = bytearray([1, 0, 0, 1, 0, 255, 255, 255]) 10 | print(utils.from_uint32_buffer(a)) 11 | a = bytearray([1, 0, 0, 1, 0, 255, 255, 255]) 12 | print(utils.from_int32_buffer(a)) 13 | 14 | a = bytearray([1, 0, 0, 1, 0, 0, 255, 255]) 15 | print(utils.from_uint32_buffer(a)) 16 | a = bytearray([1, 0, 0, 1, 0, 0, 255, 255]) 17 | print(utils.from_int32_buffer(a)) 18 | 19 | a = bytearray([1, 0, 0, 0, 0, 0, 0, 1]) 20 | print(utils.from_uint32_buffer(a)) 21 | a = bytearray([1, 0, 0, 0, 0, 0, 0, 1]) 22 | print(utils.from_int32_buffer(a)) 23 | -------------------------------------------------------------------------------- /tests/2d/utils/from_buffer.py.exp: -------------------------------------------------------------------------------- 1 | array([1.0, 256.0, 65280.0, 65535.0], dtype=float64) 2 | array([1.0, 256.0, -256.0, -1.0], dtype=float64) 3 | array([16777217.0, 4294967040.0], dtype=float64) 4 | array([16777217.0, -256.0], dtype=float64) 5 | array([16777217.0, 4294901760.0], dtype=float64) 6 | array([16777217.0, -65536.0], dtype=float64) 7 | array([1.0, 16777216.0], dtype=float64) 8 | array([1.0, 16777216.0], dtype=float64) 9 | -------------------------------------------------------------------------------- /tests/3d/complex/complex_exp.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(4), dtype=dtype) 12 | b = a.reshape((2, 2)) 13 | print('\narray:\n', a) 14 | print('\nexponential:\n', np.exp(a)) 15 | print('\narray:\n', b) 16 | print('\nexponential:\n', np.exp(b)) 17 | 18 | a = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 19 | b = np.array([[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]], dtype=np.complex) 20 | c = np.array([[[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]], [[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]]], dtype=np.complex) 21 | 22 | for m in (a, b, c): 23 | print('\n\narray:\n', m) 24 | print('\nexponential:\n', np.exp(m)) 25 | -------------------------------------------------------------------------------- /tests/3d/complex/complex_sqrt.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(8), dtype=dtype) 12 | b = a.reshape((2, 2, 2)) 13 | outtype = np.float if dtype is not np.complex else np.complex 14 | print('\narray:\n', a) 15 | print('\nsquare root:\n', np.sqrt(a, dtype=outtype)) 16 | print('\narray:\n', b) 17 | print('\nsquare root:\n', np.sqrt(b, dtype=outtype)) 18 | 19 | 20 | a = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 21 | b = np.array([0, 1j, 2+2j, 3-3j] * 2, dtype=np.complex).reshape((2, 4)) 22 | c = np.array([0, 1j, 2+2j, 3-3j] * 2, dtype=np.complex).reshape((2, 2, 2)) 23 | 24 | for m in (a, b, c): 25 | print('\n\narray:\n', m) 26 | print('\nsquare root:\n', np.sqrt(m, dtype=np.complex)) 27 | -------------------------------------------------------------------------------- /tests/3d/complex/imag_real.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(8), dtype=dtype) 12 | print('\narray:\n', a) 13 | print('\nreal part:\n', np.real(a)) 14 | print('\nimaginary part:\n', np.imag(a)) 15 | for m in (a.reshape((2, 4)), a.reshape((2, 2, 2))): 16 | print('\narray:\n', m) 17 | print('\nreal part:\n', np.real(m)) 18 | print('\nimaginary part:\n', np.imag(m), '\n') 19 | 20 | 21 | a = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 22 | b = np.array([[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]], dtype=np.complex) 23 | c = np.array([[[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]], [[0, 1j, 2+2j, 3-3j], [0, 1j, 2+2j, 3-3j]]], dtype=np.complex) 24 | 25 | for m in (a, b, c): 26 | print('\n\narray:\n', m) 27 | print('\nreal part:\n', np.real(m)) 28 | print('\nimaginary part:\n', np.imag(m)) 29 | -------------------------------------------------------------------------------- /tests/3d/numpy/create.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | print(sum(np.ones((3,2,4)))) 3 | -------------------------------------------------------------------------------- /tests/3d/numpy/create.py.exp: -------------------------------------------------------------------------------- 1 | array([[3.0, 3.0, 3.0, 3.0], 2 | [3.0, 3.0, 3.0, 3.0]], dtype=float64) 3 | -------------------------------------------------------------------------------- /tests/4d/complex/complex_exp.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(4), dtype=dtype) 12 | b = a.reshape((2, 2)) 13 | print('\narray:\n', a) 14 | print('\nexponential:\n', np.exp(a)) 15 | print('\narray:\n', b) 16 | print('\nexponential:\n', np.exp(b)) 17 | 18 | 19 | a = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 20 | b = np.array([0, 1j, 2+2j, 3-3j] * 2, dtype=np.complex).reshape((2, 4)) 21 | c = np.array([0, 1j, 2+2j, 3-3j] * 2, dtype=np.complex).reshape((2, 2, 2)) 22 | d = np.array([0, 1j, 2+2j, 3-3j] * 4, dtype=np.complex).reshape((2, 2, 2, 2)) 23 | 24 | for m in (a, b, c, d): 25 | print('\n\narray:\n', m) 26 | print('\nexponential:\n', np.exp(m)) 27 | -------------------------------------------------------------------------------- /tests/4d/complex/complex_sqrt.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(16), dtype=dtype) 12 | b = a.reshape((2, 2, 2, 2)) 13 | outtype = np.float if dtype is not np.complex else np.complex 14 | print('\narray:\n', a) 15 | print('\nsquare root:\n', np.sqrt(a, dtype=outtype)) 16 | print('\narray:\n', b) 17 | print('\nsquare root:\n', np.sqrt(b, dtype=outtype)) 18 | 19 | 20 | a = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 21 | b = np.array([0, 1j, 2+2j, 3-3j] * 2, dtype=np.complex).reshape((2, 4)) 22 | c = np.array([0, 1j, 2+2j, 3-3j] * 2, dtype=np.complex).reshape((2, 2, 2)) 23 | d = np.array([0, 1j, 2+2j, 3-3j] * 4, dtype=np.complex).reshape((2, 2, 2, 2)) 24 | 25 | for m in (a, b, c, d): 26 | print('\n\narray:\n', m) 27 | print('\nsquare root:\n', np.sqrt(m, dtype=np.complex)) 28 | -------------------------------------------------------------------------------- /tests/4d/complex/imag_real.py: -------------------------------------------------------------------------------- 1 | # this test is meaningful only, when the firmware supports complex arrays 2 | 3 | try: 4 | from ulab import numpy as np 5 | except: 6 | import numpy as np 7 | 8 | dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float, np.complex) 9 | 10 | for dtype in dtypes: 11 | a = np.array(range(16), dtype=dtype) 12 | print('\narray:\n', a) 13 | print('\nreal part:\n', np.real(a)) 14 | print('\nimaginary part:\n', np.imag(a)) 15 | for m in (a.reshape((4, 4)), a.reshape((2, 2, 4)), a.reshape((2, 2, 2, 2))): 16 | print('\narray:\n', m) 17 | print('\nreal part:\n', np.real(m)) 18 | print('\nimaginary part:\n', np.imag(m), '\n') 19 | 20 | 21 | a = np.array([0, 1j, 2+2j, 3-3j], dtype=np.complex) 22 | b = np.array([0, 1j, 2+2j, 3-3j] * 2, dtype=np.complex).reshape((2, 4)) 23 | c = np.array([0, 1j, 2+2j, 3-3j] * 2, dtype=np.complex).reshape((2, 2, 2)) 24 | d = np.array([0, 1j, 2+2j, 3-3j] * 4, dtype=np.complex).reshape((2, 2, 2, 2)) 25 | 26 | for m in (a, b, c, d): 27 | print('\n\narray:\n', m) 28 | print('\nreal part:\n', np.real(m)) 29 | print('\nimaginary part:\n', np.imag(m)) -------------------------------------------------------------------------------- /tests/4d/numpy/create.py: -------------------------------------------------------------------------------- 1 | from ulab import numpy as np 2 | print(sum(np.ones((3,4,2,5)))) 3 | -------------------------------------------------------------------------------- /tests/4d/numpy/create.py.exp: -------------------------------------------------------------------------------- 1 | array([[[3.0, 3.0, 3.0, 3.0, 3.0], 2 | [3.0, 3.0, 3.0, 3.0, 3.0]], 3 | 4 | [[3.0, 3.0, 3.0, 3.0, 3.0], 5 | [3.0, 3.0, 3.0, 3.0, 3.0]], 6 | 7 | [[3.0, 3.0, 3.0, 3.0, 3.0], 8 | [3.0, 3.0, 3.0, 3.0, 3.0]], 9 | 10 | [[3.0, 3.0, 3.0, 3.0, 3.0], 11 | [3.0, 3.0, 3.0, 3.0, 3.0]]], dtype=float64) 12 | --------------------------------------------------------------------------------