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/latest/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/latest/lib/python3.12/site-packages/finch/juliapkg.json [juliapkg] Found dependencies: /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/lib/python3.12/site-packages/juliacall/juliapkg.json [juliapkg] Found dependencies: /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/lib/python3.12/site-packages/juliapkg/juliapkg.json [juliapkg] Locating Julia =1.10.0, ^1.10.3 [juliapkg] Querying Julia versions from https://julialang-s3.julialang.org/bin/versions.json [juliapkg] WARNING: About to install Julia 1.11.4 to /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/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.4-linux-x86_64.tar.gz
download complete [juliapkg] Verifying download
[juliapkg] Installing Julia 1.11.4 to /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/julia_env/pyjuliapkg/install
[juliapkg] Using Julia 1.11.4 at /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/julia_env/pyjuliapkg/install/bin/julia [juliapkg] Using Julia project at /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/julia_env [juliapkg] Installing packages: julia> import Pkg julia> Pkg.Registry.update() julia> Pkg.add([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.PackageSpec(name="PythonCall", uuid="6099a3de-0909-46bc-b1f4-468b9a2dfc0d")]) julia> Pkg.resolve() julia> Pkg.precompile()
Installing known registries into `~/.julia`
Added `General` registry to ~/.julia/registries
Updating registry at `~/.julia/registries/General.toml`
Resolving package versions...
Installed libaec_jll ────────────────── v1.1.3+0 Installed MPICH_jll ─────────────────── v4.3.0+1 Installed MicroMamba ────────────────── v0.1.14 Installed IrrationalConstants ───────── v0.2.4 Installed Rmath ─────────────────────── v0.8.0 Installed AutoHashEquals ────────────── v2.2.0 Installed HypergeometricFunctions ───── v0.3.28 Installed Scratch ───────────────────── v1.2.1 Installed PtrArrays ─────────────────── v1.3.0 Installed StatsFuns ─────────────────── v1.4.0 Installed NPZ ───────────────────────── v0.4.3 Installed IterTools ─────────────────── v1.10.0 Installed JSON3 ─────────────────────── v1.14.2 Installed JSON ──────────────────────── v0.21.4 Installed Preferences ───────────────── v1.4.3 Installed PythonCall ────────────────── v0.9.24
Installed OpenSSL_jll ───────────────── v3.0.16+0 Installed Hwloc_jll ─────────────────── v2.12.0+0 Installed TableTraits ───────────────── v1.0.1 Installed PDMats ────────────────────── v0.11.34 Installed DataAPI ───────────────────── v1.16.0 Installed Parsers ───────────────────── v2.8.1 Installed Tables ────────────────────── v1.12.0 Installed SpecialFunctions ──────────── v2.5.0 Installed MicrosoftMPI_jll ──────────── v10.1.4+3 Installed SyntaxInterface ───────────── v0.2.1 Installed OpenMPI_jll ───────────────── v5.0.7+2 Installed JLLWrappers ───────────────── v1.7.0 Installed Pidfile ───────────────────── v1.3.0 Installed ZipFile ───────────────────── v0.10.1 Installed PrecompileTools ───────────── v1.2.1 Installed micromamba_jll ────────────── v1.5.8+0
Installed DataValueInterfaces ───────── v1.0.0 Installed IteratorInterfaceExtensions ─ v1.0.0 Installed OrderedCollections ────────── v1.8.0 Installed RewriteTools ──────────────── v0.2.4 Installed AbstractTrees ─────────────── v0.4.5 Installed FileIO ────────────────────── v1.17.0 Installed TensorMarket ──────────────── v0.2.0 Installed FillArrays ────────────────── v1.13.0 Installed MPIPreferences ────────────── v0.1.11 Installed Reexport ──────────────────── v1.2.2 Installed QuadGK ────────────────────── v2.11.2
Installed MPItrampoline_jll ─────────── v5.5.3+0 Installed HDF5 ──────────────────────── v0.17.2 Installed Finch ─────────────────────── v1.2.3 Installed Rmath_jll ─────────────────── v0.5.1+0 Installed Statistics ────────────────── v1.11.1 Installed Requires ──────────────────── v1.3.1 Installed DataStructures ────────────── v0.18.22 Installed AliasTables ───────────────── v1.1.3 Installed LogExpFunctions ───────────── v0.3.29 Installed MacroTools ────────────────── v0.5.15 Installed UnsafePointers ────────────── v1.0.0 Installed Distributions ─────────────── v0.25.118
Installed OpenSpecFun_jll ───────────── v0.5.6+0 Installed Compat ────────────────────── v4.16.0 Installed CondaPkg ──────────────────── v0.2.28 Installed StatsAPI ──────────────────── v1.7.0 Installed HDF5_jll ──────────────────── v1.14.6+0 Installed UnsafeAtomics ─────────────── v0.3.0 Installed StatsBase ─────────────────── v0.34.4 Installed SortingAlgorithms ─────────── v1.2.1 Installed Missings ──────────────────── v1.2.0 Installed pixi_jll ──────────────────── v0.41.3+0 Installed StructTypes ───────────────── v1.11.0 Installed DocStringExtensions ───────── v0.9.4
Updating `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/julia_env/Project.toml` [9177782c] + Finch v1.2.3 [f67ccb44] + HDF5 v0.17.2 [15e1cf62] + NPZ v0.4.3 [6099a3de] + PythonCall v0.9.24 [8b7d4fe7] + TensorMarket v0.2.0 Updating `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/julia_env/Manifest.toml`
[1520ce14] + AbstractTrees v0.4.5 [66dad0bd] + AliasTables v1.1.3 [15f4f7f2] + AutoHashEquals v2.2.0 [34da2185] + Compat v4.16.0 [992eb4ea] + CondaPkg v0.2.28 [9a962f9c] + DataAPI v1.16.0 [864edb3b] + DataStructures v0.18.22 [e2d170a0] + DataValueInterfaces v1.0.0 [31c24e10] + Distributions v0.25.118 [ffbed154] + DocStringExtensions v0.9.4 [5789e2e9] + FileIO v1.17.0 [1a297f60] + FillArrays v1.13.0 [9177782c] + Finch v1.2.3 [f67ccb44] + HDF5 v0.17.2 [34004b35] + HypergeometricFunctions v0.3.28 [92d709cd] + IrrationalConstants v0.2.4 [c8e1da08] + IterTools v1.10.0 [82899510] + IteratorInterfaceExtensions v1.0.0 [692b3bcd] + JLLWrappers v1.7.0 [682c06a0] + JSON v0.21.4 [0f8b85d8] + JSON3 v1.14.2 [2ab3a3ac] + LogExpFunctions v0.3.29 [3da0fdf6] + MPIPreferences v0.1.11 [1914dd2f] + MacroTools v0.5.15 [0b3b1443] + MicroMamba v0.1.14 [e1d29d7a] + Missings v1.2.0 [15e1cf62] + NPZ v0.4.3 [bac558e1] + OrderedCollections v1.8.0 [90014a1f] + PDMats v0.11.34 [69de0a69] + Parsers v2.8.1 [fa939f87] + Pidfile v1.3.0 ⌅ [aea7be01] + PrecompileTools v1.2.1 [21216c6a] + Preferences v1.4.3 [43287f4e] + PtrArrays v1.3.0 [6099a3de] + PythonCall v0.9.24 [1fd47b50] + QuadGK v2.11.2 [189a3867] + Reexport v1.2.2 [ae029012] + Requires v1.3.1 [5969e224] + RewriteTools v0.2.4 [79098fc4] + Rmath v0.8.0 [6c6a2e73] + Scratch v1.2.1 [a2af1166] + SortingAlgorithms v1.2.1 [276daf66] + SpecialFunctions v2.5.0 [10745b16] + Statistics v1.11.1 [82ae8749] + StatsAPI v1.7.0 [2913bbd2] + StatsBase v0.34.4 [4c63d2b9] + StatsFuns v1.4.0 [856f2bd8] + StructTypes v1.11.0 [b33eeca9] + SyntaxInterface v0.2.1 [3783bdb8] + TableTraits v1.0.1 [bd369af6] + Tables v1.12.0 [8b7d4fe7] + TensorMarket v0.2.0 [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.12.0+0 [7cb0a576] + MPICH_jll v4.3.0+1 [f1f71cc9] + MPItrampoline_jll v5.5.3+0 [9237b28f] + MicrosoftMPI_jll v10.1.4+3 [fe0851c0] + OpenMPI_jll v5.0.7+2 [458c3c95] + OpenSSL_jll v3.0.16+0 [efe28fd5] + OpenSpecFun_jll v0.5.6+0 [f50d1b31] + Rmath_jll v0.5.1+0 [477f73a3] + libaec_jll v1.1.3+0 [f8abcde7] + micromamba_jll v1.5.8+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.1+4 [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...
768.6 ms ✓ TensorMarket 782.6 ms ✓ AutoHashEquals
989.8 ms ✓ OrderedCollections
594.1 ms ✓ DataValueInterfaces
598.3 ms ✓ Reexport
999.7 ms ✓ DocStringExtensions 833.6 ms ✓ AbstractTrees 1750.7 ms ✓ FillArrays
628.8 ms ✓ UnsafePointers
938.4 ms ✓ Statistics
544.6 ms ✓ IteratorInterfaceExtensions
610.6 ms ✓ DataAPI
2579.2 ms ✓ IrrationalConstants 789.6 ms ✓ StatsAPI
947.3 ms ✓ IterTools
3231.1 ms ✓ UnsafeAtomics 635.8 ms ✓ SyntaxInterface
644.1 ms ✓ PtrArrays
898.7 ms ✓ Requires
1209.3 ms ✓ ZipFile
649.2 ms ✓ Scratch 1718.3 ms ✓ StructTypes
792.5 ms ✓ Pidfile 945.6 ms ✓ SuiteSparse
5544.2 ms ✓ MacroTools
1126.8 ms ✓ Compat
1476.9 ms ✓ Preferences
1250.0 ms ✓ FillArrays → FillArraysSparseArraysExt
1141.8 ms ✓ Statistics → SparseArraysExt 828.3 ms ✓ FillArrays → FillArraysStatisticsExt 490.8 ms ✓ TableTraits
788.6 ms ✓ Missings 886.4 ms ✓ RewriteTools
1263.0 ms ✓ LogExpFunctions
921.3 ms ✓ AliasTables
603.4 ms ✓ Compat → CompatLinearAlgebraExt
1547.2 ms ✓ PDMats
979.2 ms ✓ MPIPreferences
732.4 ms ✓ PrecompileTools
969.7 ms ✓ JLLWrappers
1531.9 ms ✓ Tables
1112.3 ms ✓ FillArrays → FillArraysPDMatsExt
3118.8 ms ✓ DataStructures
1313.8 ms ✓ OpenSSL_jll
993.7 ms ✓ Rmath_jll
992.5 ms ✓ MicrosoftMPI_jll
1104.8 ms ✓ Hwloc_jll 10867.1 ms ✓ FileIO
1999.1 ms ✓ MPItrampoline_jll 2139.8 ms ✓ micromamba_jll
1072.4 ms ✓ OpenSpecFun_jll
2142.3 ms ✓ pixi_jll
1199.8 ms ✓ libaec_jll
971.4 ms ✓ SortingAlgorithms
2105.0 ms ✓ QuadGK 1641.7 ms ✓ Rmath
2108.2 ms ✓ OpenMPI_jll
2431.1 ms ✓ MPICH_jll
2877.1 ms ✓ NPZ
4974.7 ms ✓ MicroMamba
5437.6 ms ✓ SpecialFunctions
4577.5 ms ✓ StatsBase
3172.6 ms ✓ HDF5_jll
1981.1 ms ✓ HypergeometricFunctions
3947.5 ms ✓ StatsFuns
9667.3 ms ✓ Distributions
2533.7 ms ✓ Distributions → DistributionsTestExt
17236.4 ms ✓ HDF5
48979.0 ms ✓ Parsers
2090.4 ms ✓ JSON
17406.7 ms ✓ JSON3
6453.1 ms ✓ CondaPkg
24422.9 ms ✓ PythonCall
276855.8 ms ✓ Finch
4753.8 ms ✓ Finch → SparseArraysExt
6240.4 ms ✓ Finch → HDF5Ext
7202.3 ms ✓ Finch → NPZExt
3221.1 ms ✓ Finch → TensorMarketExt 78 dependencies successfully precompiled in 355 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 5 seconds) │ [ Info: Precompiling common tensor operations... │ │ [ Info: Precompiling common tensor operations... (estimated: 3 minutes and 3 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 44 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 44 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 15 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 0 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 43 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 29 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 19 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 9 seconds) │ │ [ Info: Done! └ ┌ MicroMamba │ Downloading artifact: micromamba └ ┌ CondaPkg │ Downloading artifact: pixi └
No Changes to `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/julia_env/Project.toml` No Changes to `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/latest/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())