Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a constant rhs in @tensor #111

Open
feanor12 opened this issue Oct 30, 2021 · 9 comments
Open

Using a constant rhs in @tensor #111

feanor12 opened this issue Oct 30, 2021 · 9 comments

Comments

@feanor12
Copy link

Is it possible to assign a constant using the @tensor macro?

Example:

C=zeros(3,3,3)
@tensor begin
    C[i,i,i] = 6
end
@Jutho
Copy link
Owner

Jutho commented Nov 1, 2021

No that is not supported. TensorOperations is rather restricted. It was motivated by physics applications and only handles expressions which can be thought of as being basis independent. I guess alternatives like Tullio.jl are more flexible and probably more useful for general data science / ML applications.

@AuroraDysis
Copy link

AuroraDysis commented Nov 13, 2024

I would like to share some insights on this issue.

In my field, specifically in the areas of strong gravity and numerical relativity, constants play a role. To illustrate, consider the following example equation used in numerical relativity:

$\left(\partial_t-\beta^l \partial_l\right) \psi=-\frac{(N-2) \psi}{2 N}\left(\alpha K-\partial_k \beta^k\right),$

Regrettably, I have found that Tullio.jl and other similar alternatives are not suitable for physics applications. The main reason is their non-standard implementation of einsum, as discussed in this thread: ahwillia/Einsum.jl#39.

@Jutho
Copy link
Owner

Jutho commented Nov 13, 2024

I am not sure I understand how the example equation is related to this question. In any case, for your question, can't you just do fill!(C, 6) before entering the @tensor macro block?

@AuroraDysis
Copy link

I apologize for any confusion. I used a random equation from a book to avoid presenting overly complex ones. My intention was to highlight the importance of scalar fields in physics, as they appear everywhere in my field. I would greatly appreciate it if TensorOperations.jl could support constants (scalar fields) in the expression.

@lkdvos
Copy link
Collaborator

lkdvos commented Nov 13, 2024

You could maybe have a look at FillArrays.jl https://github.com/JuliaArrays/FillArrays.jl which could do exactly what you need, with minimal performance penalties

@feanor12
Copy link
Author

I am not sure I understand how the example equation is related to this question. In any case, for your question, can't you just do fill!(C, 6) before entering the @tensor macro block?

This fills all 27 elements with 6, which is not what I wanted to achieve.

@AuroraDysis
Copy link

You could maybe have a look at FillArrays.jl https://github.com/JuliaArrays/FillArrays.jl which could do exactly what you need, with minimal performance penalties

It's a possible method, but it also requires simplifying the equation to factor out the constant (scalar field) part, which is inconvenient when dealing with a large number of equations. In relativity, the equations are typically very complex. I want to emphasize that a scalar field is a zeroth-order tensor field. There's no reason for me to exclude it from tensor operations.

@Jutho
Copy link
Owner

Jutho commented Nov 13, 2024

Oh yes, I misread / was too quick. Is there any reason not to simply write the loop?

for i = 1:n
 C[i,i,i] = 6
end

It doesn't really fit into TensorOperations to handle anything with an index appearing more than two times.

TensorOperations.jl should be well suited for anything that adheres strictly to Einstein's summation convention and that thus represents a basis-independent expression. Scalar fields are fine, either as rank-zero array or as constant, e.g. @tensor S[] := ginv[i,j] * R[c,j,c,i] is a perfectly fine expression to compute the scalar curvature from the Riemann curvature and the inverse metric. I guess I still don't see what C[i,i,i] has to do with scalar fields?

@feanor12
Copy link
Author

feanor12 commented Nov 13, 2024

Is it possible to write this using Kronecker deltas?
$C_{ijk}=6 \delta_{ij} \delta_{jk}$
or
$C_{ijkl} = \lambda \delta_{ij} \delta_{kl} + \mu (\delta_{ik} \delta_{jl} + \delta_{jl}\delta_{kj})$

This could be useful for describing some "symmetric" tensors.

Here is a 2D Example, but this can get up to 4D in mechanics.
$C_{ij}=6 \delta_{ij} + 2 \delta_{j+i,1}$

# does not work
using TensorOperations
struct δ end

import Base.getindex
Base.getindex(::Type{δ},inds...) = allequal(inds)

C = zeros(2,2)
@tensor begin
   C[i,j] = 6*δ[i,j]+2*δ[i+j,1]
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants