In a new color library I’m writing there is a lot of matrix math to do with chromaticity adaptation, conversion between linear space and gamma space and conversions between color spaces in general.
Many of these conversion matrices can be computed at compile time and Nx is a great fit for this and is working well. Here’s an example:
@chromatic_adaptations %{
xyz_scaling: %{
matrix: [
[1.0000000, 0.0000000, 0.0000000],
[0.0000000, 1.0000000, 0.0000000],
[0.0000000, 0.0000000, 1.0000000]
] |> Nx.tensor(),
inverse_matrix: [
[1.0000000, 0.0000000, 0.0000000],
[0.0000000, 1.0000000, 0.0000000],
[0.0000000, 0.0000000, 1.0000000]
] |> Nx.tensor()
},
...
}
However by doing this the tensors are not “backend” aware. Color
shouldn’t impose any configuration on the user at all. Therefore I’m not sure what the right strategy is to define tensors at compile time and yet still fit into a users Nx
backend configuration.
One possible approach might be to:
- Pre-compute the tensors
- Serialize the data
- Load the data at application start and
Nx.backend_transfer/2
the data - Store the resulting tensors in :persistent_term
Thoughts and suggestions very welcome.