In [1]:
Copied!
import os
os.environ["SPARSE_BACKEND"] = "Finch"
import sparse
import numpy as np
import os
os.environ["SPARSE_BACKEND"] = "Finch"
import sparse
import numpy as np
/home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/checkouts/stable/sparse/__init__.py:24: SparseFutureWarning: Changing back-ends is a development feature, please do not rely on it in production. warnings.warn(
[juliapkg] Found dependencies: /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/lib/python3.12/site-packages/juliapkg/juliapkg.json [juliapkg] Found dependencies: /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/lib/python3.12/site-packages/juliacall/juliapkg.json [juliapkg] Found dependencies: /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/lib/python3.12/site-packages/finch/juliapkg.json [juliapkg] Locating Julia 1.10.3 - 1.11 [juliapkg] Querying Julia versions from https://julialang-s3.julialang.org/bin/versions.json [juliapkg] WARNING: About to install Julia 1.11.9 to /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/pyjuliapkg/install. [juliapkg] If you use juliapkg in more than one environment, you are likely to [juliapkg] have Julia installed in multiple locations. It is recommended to [juliapkg] install JuliaUp (https://github.com/JuliaLang/juliaup) or Julia [juliapkg] (https://julialang.org/downloads) yourself. [juliapkg] Downloading Julia from https://julialang-s3.julialang.org/bin/linux/x64/1.11/julia-1.11.9-linux-x86_64.tar.gz
download complete [juliapkg] Verifying download [juliapkg] Installing Julia 1.11.9 to /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/pyjuliapkg/install
[juliapkg] Using Julia 1.11.9 at /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/pyjuliapkg/install/bin/julia
[juliapkg] Using Julia project at /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env
[juliapkg] Writing Project.toml:
| [deps]
| PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
| OpenSSL_jll = "458c3c95-2e84-50aa-8efc-19380b2a3a95"
| Finch = "9177782c-1635-4eb9-9bfb-d9dfa25e6bce"
| HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
| NPZ = "15e1cf62-19b3-5cfa-8e77-841668bca605"
| TensorMarket = "8b7d4fe7-0b45-4d0d-9dd8-5cc9b23b4b77"
|
| [compat]
| PythonCall = "=0.9.31"
| OpenSSL_jll = "~3.0"
| Finch = "^1.2.10"
| HDF5 = "^0.17.2"
| NPZ = "^0.4.3"
| TensorMarket = "^0.2"
[juliapkg] Installing packages:
| import Pkg
| Pkg.Registry.update()
| Pkg.add([
| Pkg.PackageSpec(name="PythonCall", uuid="6099a3de-0909-46bc-b1f4-468b9a2dfc0d"),
| Pkg.PackageSpec(name="OpenSSL_jll", uuid="458c3c95-2e84-50aa-8efc-19380b2a3a95"),
| Pkg.PackageSpec(name="Finch", uuid="9177782c-1635-4eb9-9bfb-d9dfa25e6bce"),
| Pkg.PackageSpec(name="HDF5", uuid="f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"),
| Pkg.PackageSpec(name="NPZ", uuid="15e1cf62-19b3-5cfa-8e77-841668bca605"),
| Pkg.PackageSpec(name="TensorMarket", uuid="8b7d4fe7-0b45-4d0d-9dd8-5cc9b23b4b77"),
| ])
| Pkg.resolve()
| Pkg.precompile()
Installing known registries into `~/.julia`
Added `General` registry to ~/.julia/registries
Updating registry at `~/.julia/registries/General.toml` Resolving package versions...
Installed MPICH_jll ─────────────────── v4.3.2+0 Installed XML2_jll ──────────────────── v2.13.9+0 Installed libaec_jll ────────────────── v1.1.5+0 Installed IrrationalConstants ───────── v0.2.6 Installed AutoHashEquals ────────────── v2.2.0 Installed MicroMamba ────────────────── v0.1.15 Installed Rmath ─────────────────────── v0.9.0 Installed Scratch ───────────────────── v1.3.0 Installed HypergeometricFunctions ───── v0.3.28 Installed Xorg_libpciaccess_jll ─────── v0.18.1+0 Installed PtrArrays ─────────────────── v1.3.0 Installed NPZ ───────────────────────── v0.4.3 Installed StatsFuns ─────────────────── v1.5.2
Installed JSON ──────────────────────── v1.4.0 Installed Preferences ───────────────── v1.5.1 Installed PythonCall ────────────────── v0.9.31 Installed OpenSSL_jll ───────────────── v3.0.16+0 Installed TableTraits ───────────────── v1.0.1 Installed PDMats ────────────────────── v0.11.37 Installed Parsers ───────────────────── v2.8.3 Installed Tables ────────────────────── v1.12.1 Installed IterTools ─────────────────── v1.10.0 Installed SpecialFunctions ──────────── v2.7.1 Installed DataAPI ───────────────────── v1.16.0 Installed MicrosoftMPI_jll ──────────── v10.1.4+3 Installed OpenMPI_jll ───────────────── v5.0.9+0 Installed JLLWrappers ───────────────── v1.7.1 Installed ZipFile ───────────────────── v0.10.1 Installed Pidfile ───────────────────── v1.3.0 Installed SyntaxInterface ───────────── v0.2.1
Installed micromamba_jll ────────────── v2.3.1+0 Installed DataValueInterfaces ───────── v1.0.0 Installed Hwloc_jll ─────────────────── v2.13.0+0 Installed Libiconv_jll ──────────────── v1.18.0+0 Installed IteratorInterfaceExtensions ─ v1.0.0 Installed AbstractTrees ─────────────── v0.4.5 Installed PrecompileTools ───────────── v1.2.1
Installed OrderedCollections ────────── v1.8.1 Installed RewriteTools ──────────────── v0.2.4 Installed TensorMarket ──────────────── v0.2.2 Installed QuadGK ────────────────────── v2.11.2 Installed FillArrays ────────────────── v1.16.0 Installed FileIO ────────────────────── v1.18.0 Installed Finch ─────────────────────── v1.2.12 Installed MPIPreferences ────────────── v0.1.11 Installed Reexport ──────────────────── v1.2.2 Installed Statistics ────────────────── v1.11.1 Installed Rmath_jll ─────────────────── v0.5.1+0 Installed DataStructures ────────────── v0.19.3 Installed HDF5 ──────────────────────── v0.17.2 Installed LogExpFunctions ───────────── v0.3.29 Installed Requires ──────────────────── v1.3.1 Installed AliasTables ───────────────── v1.1.3
Installed MacroTools ────────────────── v0.5.16 Installed UnsafePointers ────────────── v1.0.0 Installed StatsAPI ──────────────────── v1.8.0 Installed MPItrampoline_jll ─────────── v5.5.5+0 Installed Compat ────────────────────── v4.18.1 Installed OpenSpecFun_jll ───────────── v0.5.6+0 Installed Distributions ─────────────── v0.25.123 Installed UnsafeAtomics ─────────────── v0.3.0 Installed HDF5_jll ──────────────────── v1.14.6+0 Installed Missings ──────────────────── v1.2.0 Installed SortingAlgorithms ─────────── v1.2.2 Installed StatsBase ─────────────────── v0.34.10 Installed StructUtils ───────────────── v2.6.3 Installed DocStringExtensions ───────── v0.9.5 Installed pixi_jll ──────────────────── v0.41.3+0
Installed CondaPkg ──────────────────── v0.2.34
Updating `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/Project.toml`
[9177782c] + Finch v1.2.12
[f67ccb44] + HDF5 v0.17.2
[15e1cf62] + NPZ v0.4.3
[6099a3de] + PythonCall v0.9.31
[8b7d4fe7] + TensorMarket v0.2.2
⌅ [458c3c95] + OpenSSL_jll v3.0.16+0
Updating `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/Manifest.toml`
[1520ce14] + AbstractTrees v0.4.5
[66dad0bd] + AliasTables v1.1.3
[15f4f7f2] + AutoHashEquals v2.2.0
[34da2185] + Compat v4.18.1
[992eb4ea] + CondaPkg v0.2.34
[9a962f9c] + DataAPI v1.16.0
[864edb3b] + DataStructures v0.19.3
[e2d170a0] + DataValueInterfaces v1.0.0
[31c24e10] + Distributions v0.25.123
[ffbed154] + DocStringExtensions v0.9.5
[5789e2e9] + FileIO v1.18.0
[1a297f60] + FillArrays v1.16.0
[9177782c] + Finch v1.2.12
[f67ccb44] + HDF5 v0.17.2
[34004b35] + HypergeometricFunctions v0.3.28
[92d709cd] + IrrationalConstants v0.2.6
[c8e1da08] + IterTools v1.10.0
[82899510] + IteratorInterfaceExtensions v1.0.0
[692b3bcd] + JLLWrappers v1.7.1
[682c06a0] + JSON v1.4.0
[2ab3a3ac] + LogExpFunctions v0.3.29
[3da0fdf6] + MPIPreferences v0.1.11
[1914dd2f] + MacroTools v0.5.16
[0b3b1443] + MicroMamba v0.1.15
[e1d29d7a] + Missings v1.2.0
[15e1cf62] + NPZ v0.4.3
[bac558e1] + OrderedCollections v1.8.1
[90014a1f] + PDMats v0.11.37
[69de0a69] + Parsers v2.8.3
[fa939f87] + Pidfile v1.3.0
⌅ [aea7be01] + PrecompileTools v1.2.1
[21216c6a] + Preferences v1.5.1
[43287f4e] + PtrArrays v1.3.0
[6099a3de] + PythonCall v0.9.31
[1fd47b50] + QuadGK v2.11.2
[189a3867] + Reexport v1.2.2
[ae029012] + Requires v1.3.1
[5969e224] + RewriteTools v0.2.4
[79098fc4] + Rmath v0.9.0
[6c6a2e73] + Scratch v1.3.0
[a2af1166] + SortingAlgorithms v1.2.2
[276daf66] + SpecialFunctions v2.7.1
[10745b16] + Statistics v1.11.1
[82ae8749] + StatsAPI v1.8.0
[2913bbd2] + StatsBase v0.34.10
[4c63d2b9] + StatsFuns v1.5.2
[ec057cc2] + StructUtils v2.6.3
[b33eeca9] + SyntaxInterface v0.2.1
[3783bdb8] + TableTraits v1.0.1
[bd369af6] + Tables v1.12.1
[8b7d4fe7] + TensorMarket v0.2.2
[013be700] + UnsafeAtomics v0.3.0
[e17b2a0c] + UnsafePointers v1.0.0
[a5390f91] + ZipFile v0.10.1
⌅ [0234f1f7] + HDF5_jll v1.14.6+0
[e33a78d0] + Hwloc_jll v2.13.0+0
[94ce4f54] + Libiconv_jll v1.18.0+0
⌅ [7cb0a576] + MPICH_jll v4.3.2+0
[f1f71cc9] + MPItrampoline_jll v5.5.5+0
[9237b28f] + MicrosoftMPI_jll v10.1.4+3
[fe0851c0] + OpenMPI_jll v5.0.9+0
⌅ [458c3c95] + OpenSSL_jll v3.0.16+0
[efe28fd5] + OpenSpecFun_jll v0.5.6+0
[f50d1b31] + Rmath_jll v0.5.1+0
⌅ [02c8fc9c] + XML2_jll v2.13.9+0
[a65dc6b1] + Xorg_libpciaccess_jll v0.18.1+0
[477f73a3] + libaec_jll v1.1.5+0
[f8abcde7] + micromamba_jll v2.3.1+0
[4d7b5844] + pixi_jll v0.41.3+0
[0dad84c5] + ArgTools v1.1.2
[56f22d72] + Artifacts v1.11.0
[2a0f44e3] + Base64 v1.11.0
[ade2ca70] + Dates v1.11.0
[f43a241f] + Downloads v1.6.0
[7b1f6079] + FileWatching v1.11.0
[b77e0a4c] + InteractiveUtils v1.11.0
[4af54fe1] + LazyArtifacts v1.11.0
[b27032c2] + LibCURL v0.6.4
[76f85450] + LibGit2 v1.11.0
[8f399da3] + Libdl v1.11.0
[37e2e46d] + LinearAlgebra v1.11.0
[56ddb016] + Logging v1.11.0
[d6f4376e] + Markdown v1.11.0
[a63ad114] + Mmap v1.11.0
[ca575930] + NetworkOptions v1.2.0
[44cfe95a] + Pkg v1.11.0
[de0858da] + Printf v1.11.0
[9a3f8284] + Random v1.11.0
[ea8e919c] + SHA v0.7.0
[9e88b42a] + Serialization v1.11.0
[2f01184e] + SparseArrays v1.11.0
[4607b0f0] + SuiteSparse
[fa267f1f] + TOML v1.0.3
[a4e569a6] + Tar v1.10.0
[8dfed614] + Test v1.11.0
[cf7118a7] + UUIDs v1.11.0
[4ec0a83e] + Unicode v1.11.0
[e66e0078] + CompilerSupportLibraries_jll v1.1.1+0
[deac9b47] + LibCURL_jll v8.6.0+0
[e37daf67] + LibGit2_jll v1.7.2+0
[29816b5a] + LibSSH2_jll v1.11.0+1
[c8ffd9c3] + MbedTLS_jll v2.28.6+0
[14a3606d] + MozillaCACerts_jll v2023.12.12
[4536629a] + OpenBLAS_jll v0.3.27+1
[05823500] + OpenLibm_jll v0.8.5+0
[bea87d4a] + SuiteSparse_jll v7.7.0+0
[83775a58] + Zlib_jll v1.2.13+1
[8e850b90] + libblastrampoline_jll v5.11.0+0
[8e850ede] + nghttp2_jll v1.59.0+0
[3f19e933] + p7zip_jll v17.4.0+2
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
Precompiling project...
427.1 ms ✓ TensorMarket
488.8 ms ✓ AutoHashEquals
814.9 ms ✓ OrderedCollections
334.0 ms ✓ DataValueInterfaces
343.3 ms ✓ Reexport
522.6 ms ✓ DocStringExtensions
1099.6 ms ✓ FillArrays
600.2 ms ✓ AbstractTrees
739.2 ms ✓ Statistics
503.8 ms ✓ UnsafePointers
433.8 ms ✓ IteratorInterfaceExtensions
1426.6 ms ✓ IrrationalConstants
444.0 ms ✓ DataAPI
513.5 ms ✓ StatsAPI
678.7 ms ✓ IterTools
449.1 ms ✓ SyntaxInterface 1968.5 ms ✓ UnsafeAtomics
333.9 ms ✓ PtrArrays
482.2 ms ✓ Requires
659.8 ms ✓ ZipFile
856.0 ms ✓ StructUtils
555.5 ms ✓ Scratch
565.2 ms ✓ Pidfile
646.2 ms ✓ SuiteSparse
3320.2 ms ✓ MacroTools
672.1 ms ✓ Preferences
801.9 ms ✓ Compat
691.8 ms ✓ FillArrays → FillArraysSparseArraysExt
654.0 ms ✓ Statistics → SparseArraysExt
424.3 ms ✓ TableTraits
521.7 ms ✓ FillArrays → FillArraysStatisticsExt
459.6 ms ✓ Missings
2018.5 ms ✓ DataStructures
848.2 ms ✓ LogExpFunctions
586.8 ms ✓ RewriteTools
453.5 ms ✓ AliasTables
706.1 ms ✓ MPIPreferences
952.8 ms ✓ PDMats
412.7 ms ✓ PrecompileTools
520.3 ms ✓ JLLWrappers
391.9 ms ✓ Compat → CompatLinearAlgebraExt
545.3 ms ✓ SortingAlgorithms
1007.0 ms ✓ Tables
1021.3 ms ✓ QuadGK
756.8 ms ✓ FillArrays → FillArraysPDMatsExt
608.0 ms ✓ OpenSSL_jll
603.5 ms ✓ Rmath_jll
617.7 ms ✓ MicrosoftMPI_jll
602.8 ms ✓ Libiconv_jll
6468.4 ms ✓ FileIO
1253.2 ms ✓ MPItrampoline_jll
1170.1 ms ✓ micromamba_jll
701.9 ms ✓ Xorg_libpciaccess_jll
762.2 ms ✓ OpenSpecFun_jll
1535.0 ms ✓ pixi_jll
614.4 ms ✓ libaec_jll
447.0 ms ✓ StructUtils → StructUtilsTablesExt
885.6 ms ✓ Rmath
631.3 ms ✓ XML2_jll
2603.6 ms ✓ StatsBase
1676.9 ms ✓ NPZ
3174.5 ms ✓ MicroMamba
743.6 ms ✓ Hwloc_jll
3185.3 ms ✓ SpecialFunctions
729.0 ms ✓ PDMats → StatsBaseExt
1409.0 ms ✓ OpenMPI_jll
1564.2 ms ✓ MPICH_jll
1142.1 ms ✓ HypergeometricFunctions
2166.2 ms ✓ HDF5_jll
1843.1 ms ✓ StatsFuns
19838.4 ms ✓ Parsers
5825.4 ms ✓ Distributions
1483.8 ms ✓ Distributions → DistributionsTestExt
4855.5 ms ✓ JSON
9766.0 ms ✓ HDF5
4467.8 ms ✓ CondaPkg
14636.8 ms ✓ PythonCall
215274.4 ms ✓ Finch
2857.0 ms ✓ Finch → SparseArraysExt
4016.6 ms ✓ Finch → HDF5Ext 4127.2 ms ✓ Finch → NPZExt
2243.6 ms ✓ Finch → TensorMarketExt
82 dependencies successfully precompiled in 258 seconds. 32 already precompiled.
3 dependencies had output during precompilation:
┌ Finch
│ [ Info: Running enhanced Finch.jl precompilation... (to disable, run `using Preferences; Preferences.set_preferences!("Finch", "precompile_workload" => false; force=true)`). See https://julialang.github.io/PrecompileTools.jl/stable/#Package-developers:-reducing-the-cost-of-precompilation-during-development for more.
│ [ Info: Precompiling common tensor formats...
│ [ Info: Precompiling common tensor formats... (estimated: 0 minutes and 3 seconds)
│ [ Info: Precompiling common tensor operations...
│
│ [ Info: Precompiling common tensor operations... (estimated: 2 minutes and 9 seconds)
│
│ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 16 seconds)
│
│ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 26 seconds)
│
│ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 2 seconds)
│
│ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 48 seconds)
│
│ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 34 seconds)
│
│ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 23 seconds)
│
│ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 15 seconds)
│
│ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 7 seconds)
│
│ [ Info: Done!
└
┌ CondaPkg
│ Downloading artifact: pixi
└
┌ MicroMamba
│ Downloading artifact: micromamba
└
No Changes to `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/Project.toml` No Changes to `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/Manifest.toml`
Detected IPython. Loading juliacall extension. See https://juliapy.github.io/PythonCall.jl/stable/compat/#IPython
Perform Operations¶
Let's create two arrays.
In [2]:
Copied!
rng = np.random.default_rng(42) # Seed for reproducibility
a = sparse.random((3, 3), density=1 / 6, random_state=rng)
b = sparse.random((3, 3), density=1 / 6, random_state=rng)
rng = np.random.default_rng(42) # Seed for reproducibility
a = sparse.random((3, 3), density=1 / 6, random_state=rng)
b = sparse.random((3, 3), density=1 / 6, random_state=rng)
Now let's matrix multiply them.
In [3]:
Copied!
c = a @ b
c = a @ b
And view the result as a (dense) NumPy array.
In [4]:
Copied!
c_dense = c.todense()
c_dense = c.todense()
Now let's do the same for other formats, and compare the results.
In [5]:
Copied!
for format in ["coo", "csr", "csc", "dense"]:
af = sparse.asarray(a, format=format)
bf = sparse.asarray(b, format=format)
cf = af @ bf
np.testing.assert_array_equal(c_dense, cf.todense())
for format in ["coo", "csr", "csc", "dense"]:
af = sparse.asarray(a, format=format)
bf = sparse.asarray(b, format=format)
cf = af @ bf
np.testing.assert_array_equal(c_dense, cf.todense())