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

Add an integer power function for second order tensors #53

Merged
merged 3 commits into from
Apr 10, 2017
Merged

Add an integer power function for second order tensors #53

merged 3 commits into from
Apr 10, 2017

Conversation

KeitaNakamura
Copy link
Contributor

@KeitaNakamura KeitaNakamura commented Apr 7, 2017

This would be useful in some cases.

julia> t = rand(Tensor{2,3});

julia> t^0  one(t)
true

julia> t^1  t
true

julia> t^2  t  t
true

julia> t^3  t  t  t
true

julia> t^-1  inv(t)
true

@KristofferC
Copy link
Collaborator

If we allowed for * we would get this atuomatically... The implementation here is a bit inefficient because they do not use the power by squaring method.

@KeitaNakamura
Copy link
Contributor Author

KeitaNakamura commented Apr 7, 2017

If we allowed for * we would get this atuomatically...

Yes, I know but I thought you don't want to allow to use * operator. So, should I follow the original julia's code?

@KristofferC
Copy link
Collaborator

I think this is fine. I don't expect large powers to be used much and we can fix it then if someone complains.

return one(S)
elseif p == -1
return inv(S)
elseif p < 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why throw domain error here? Can't you do inv(S)^(-p)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I would fix it. But might be inv(S)^p?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, sorry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, how could I avoid errors in julia v0.6?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the problem on 0.6?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually Travis would fail in "constructors" section, not in "simple math".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The errors are following:

constructors: Test Failed
Expression: eltype(tens_arr1) == (Tensors.Tensor){1, 1, Float64, N}
Evaluated: Tensors.Tensor{1,1,T,M} where M where T<:Real == Tensors.Tensor{1,1,Float64,1}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add more error message just in case.

Stacktrace:
[1] macro expansion at /home/travis/.julia/v0.6/Tensors/test/test_misc.jl:42 [inlined]
[2] macro expansion at ./test.jl:853 [inlined]
[3] macro expansion at /home/travis/.julia/v0.6/Tensors/test/runtests.jl:9 [inlined]
[4] macro expansion at /home/travis/.julia/v0.6/TimerOutputs/src/TimerOutput.jl:128 [inlined]
[5] macro expansion at /home/travis/.julia/v0.6/Tensors/test/runtests.jl:8 [inlined]
[6] anonymous at ./:?

@test t^1 ≈ t
@test t^2 ≈ t ⋅ t
@test t^3 ≈ t ⋅ t ⋅ t
@test t^-1 ≈ inv(t)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for t^-2 as well

@fredrikekre
Copy link
Member

I think we should do this with Base.literal_pow on julia06

@KristofferC
Copy link
Collaborator

Some change in Base made tests fail..

@fredrikekre
Copy link
Member

Its this:

julia> zeros(Tensor{2, 3}, 3)
3-element Array{Tensors.Tensor{2,3,T,M} where M where T<:Real,1}:
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]

before T and M where correct here

@KristofferC
Copy link
Collaborator

KristofferC commented Apr 7, 2017

julia> @code_warntype zeros(Tensor{2, 3}, 3)

julia> 

wut?

julia> @which zeros(Tensor{2, 3}, 3)
ERROR: no method found for the specified argument types
Stacktrace:
 [1] which(::Any, ::Any) at ./reflection.jl:824


julia> zeros(Tensor{2, 3}, 3)
3-element Array{Tensors.Tensor{2,3,T,M} where M where T<:Real,1}:
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]

wutwut?

@KeitaNakamura
Copy link
Contributor Author

Also

julia> methods(zeros, (Type{Tensor{2,3}}, Int))
# 0 methods for generic function "zeros":

@KristofferC
Copy link
Collaborator

Bisected to JuliaLang/julia#21183

@KristofferC KristofferC merged commit 214b886 into Ferrite-FEM:master Apr 10, 2017
@fredrikekre fredrikekre mentioned this pull request Apr 10, 2017
@KeitaNakamura KeitaNakamura deleted the int-power branch April 11, 2017 18:29
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

Successfully merging this pull request may close these issues.

3 participants