Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcc99398

Browse files
committed
Getting started
0 parents  commitcc99398

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

‎Manifest.toml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
[[Libdl]]
4+
uuid ="8f399da3-3557-5675-b5ff-fb832c97cbdb"
5+
6+
[[LinearAlgebra]]
7+
deps = ["Libdl"]
8+
uuid ="37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
9+
10+
[[Random]]
11+
deps = ["Serialization"]
12+
uuid ="9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
13+
14+
[[Serialization]]
15+
uuid ="9e88b42a-f829-5b0c-bbe9-9e923198166b"
16+
17+
[[SparseArrays]]
18+
deps = ["LinearAlgebra","Random"]
19+
uuid ="2f01184e-e22b-5df5-ae63-d93ebab69eaf"
20+
21+
[[StaticArrays]]
22+
deps = ["LinearAlgebra","Random","Statistics"]
23+
git-tree-sha1 ="db23bbf50064c582b6f2b9b043c8e7e98ea8c0c6"
24+
uuid ="90137ffa-7385-5640-81b9-e52037218182"
25+
version ="0.11.0"
26+
27+
[[Statistics]]
28+
deps = ["LinearAlgebra","SparseArrays"]
29+
uuid ="10745b16-79ce-11e8-11f9-7d13ad32a3b2"
30+
31+
[[Unitful]]
32+
deps = ["LinearAlgebra","Random"]
33+
git-tree-sha1 ="92bdf0ccfa9612b167d0adaadef832a09971ceb0"
34+
uuid ="1986cc42-f94f-5a68-af5c-568840ba703d"
35+
version ="0.17.0"

‎Project.toml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name ="UnitfulLinearAlgebra"
2+
uuid ="fbc1ec3a-669c-4419-81e4-966e190e5056"
3+
authors = ["Gustavo Goretkin <gustavo.goretkin@gmail.com>"]
4+
version ="0.1.0"
5+
6+
[deps]
7+
LinearAlgebra ="37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
8+
Unitful ="1986cc42-f94f-5a68-af5c-568840ba703d"
9+
10+
[extras]
11+
Test ="8dfed614-e22c-5e08-85e1-65c5234f0b40"
12+
StaticArrays ="90137ffa-7385-5640-81b9-e52037218182"
13+
14+
[targets]
15+
test = ["Test","StaticArrays"]

‎src/UnitfulLinearAlgebra.jl‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
module UnitfulLinearAlgebra
2+
3+
using Unitful:@u_str, FreeUnits, unit
4+
using LinearAlgebra: dot
5+
6+
# We want TagsAlongAxis to act like an AbstactVector
7+
# TagsAlongAxis{T} <: AbstractVector{Unitful.FreeUnits} means that *instances* of TagsAlongAxis act like an AbstractVector. Not what we want.
8+
struct TagsAlongAxis{T}
9+
end
10+
11+
#TODO also define `lastindex`
12+
#TODO restrict T to be tuple?
13+
Base.getindex(taa::Type{TagsAlongAxis{T}}, i)where {T}= T[i]
14+
Base.iterate(taa::Type{TagsAlongAxis{T}}, args...)where {T}=iterate(T, args...)
15+
Base.length(taa::Type{TagsAlongAxis{T}})where {T}=length(T)
16+
17+
import LinearAlgebra
18+
19+
# generic fallback won't work unless we define `iterate` on `typeof(u"m")`.
20+
# u"m" is an object which supports only `:*`. `u"m" + u"cm"` has no meaning.
21+
# to see if two `TagsAlongAxis` are compatible for contraction, give them units and check that.
22+
function LinearAlgebra.dot(v1::Type{TagsAlongAxis{UNITS1}}, v2::Type{TagsAlongAxis{UNITS2}})where {UNITS1, UNITS2}
23+
@assertlength(UNITS1)==length(UNITS2)
24+
u1_one=map(u->true* u, UNITS1)
25+
u2_one=map(u->true* u, UNITS2)
26+
27+
s= u1_one[1]* u2_one[1]
28+
for i=2:length(UNITS1)
29+
s+= u1_one[i]* u2_one[i]
30+
end
31+
returnunit(s)
32+
end
33+
34+
#TODO consider using (Holy-) Traits
35+
Multipliable= Union{Number, FreeUnits}
36+
37+
function Base.:*(a::Multipliable, v::Type{TagsAlongAxis{UNITS}})where {UNITS}
38+
return TagsAlongAxis{map(u-> a* u, UNITS)}
39+
end
40+
41+
42+
function Base.:*(v::Type{TagsAlongAxis{UNITS}}, a::Multipliable)where {UNITS}
43+
# field multiplication should be commutative, but maintain multipliation order anyway.
44+
return TagsAlongAxis{map(u-> u* a, UNITS)}
45+
end
46+
47+
struct TagsOuterProduct{T}
48+
end
49+
50+
#TODO types vs singletons. Can dispatch on types.
51+
function Base.getindex(top::Type{TagsOuterProduct{Tuple{TAA}}}, ind)where {TAA}
52+
returngetindex(TAA, ind)
53+
end
54+
55+
56+
#TODO define recursively, dispatch on Tuple{TAA1, VarArgs}
57+
function Base.getindex(top::Type{TagsOuterProduct{Tuple{TAA1, TAA2}}}, ind1, ind2)where {TAA1, TAA2}
58+
returngetindex(TAA1, ind1)*getindex(TAA2, ind2)
59+
end
60+
61+
62+
#TODO restrict TT to TT<:TagsOuterProduct
63+
struct TaggedTensor{T, N, A<:AbstractArray, TT}<:AbstractArray{T, N}
64+
x::A
65+
end
66+
67+
#TODO put in inner constructor
68+
functionTTensor(tag, x)
69+
TaggedTensor{eltype(x), ndims(x), typeof(x), tag}(x)
70+
end
71+
72+
get_tag(::TaggedTensor{T, N, A, TAG})where{T, N, A, TAG}= TAG
73+
74+
Base.size(t::TaggedTensor)=size(t.x)
75+
Base.getindex(t::TaggedTensor, ind...)= t.x[ind...]*get_tag(t)[ind...]
76+
#get_freeunits(quantity::Unitful.Quantity{T, Dim, Unit}) = Unit
77+
78+
# matrix-vector product
79+
function Base.:*(m::Type{TagsOuterProduct{Tuple{mTAA1, mTAA2}}}, v::Type{TagsOuterProduct{Tuple{vTAA}}})where {mTAA1, mTAA2, vTAA}
80+
taa= mTAA1*dot(mTAA2, vTAA)
81+
return TagsOuterProduct{Tuple{taa}}
82+
end
83+
84+
# matrix-matrix product
85+
function Base.:*(m::Type{TagsOuterProduct{Tuple{m1TAA1, m1TAA2}}}, v::Type{TagsOuterProduct{Tuple{m2TAA1, m2TAA2}}})where {m1TAA1, m1TAA2, m2TAA1, m2TAA2}
86+
return m1TAA1*dot(m1TAA2, m2TAA1)* m2TAA2
87+
end
88+
89+
function Base.:*(t1::TaggedTensor, t2::TaggedTensor)
90+
TAG1=get_tag(t1)
91+
TAG2=get_tag(t2)
92+
TAG= TAG1* TAG2
93+
t= t1.x* t2.x
94+
95+
returnTTensor(TAG, t)
96+
end
97+
98+
end# module

‎test/runtests.jl‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Test
2+
using StaticArrays
3+
using Unitful
4+
5+
using UnitfulLinearAlgebra: TagsAlongAxis, TagsOuterProduct, TaggedTensor, TTensor, get_tag
6+
7+
8+
UX= (u"m",u"m/s")
9+
UXdot= UX./u"s"
10+
11+
UXinv=map(inv, UX)
12+
13+
TX=map(typeof, UX)
14+
TXdot=map(typeof, UXdot)
15+
16+
taa_UX= TagsAlongAxis{UX}
17+
taa_UXinv= TagsAlongAxis{UXinv}
18+
taa_UXdot= TagsAlongAxis{UXdot}
19+
20+
@testset"TagsAlongAxis"begin
21+
@test taa_UX[1]==u"m"
22+
@test taa_UX[2]==u"m/s"
23+
end
24+
25+
top1= TagsOuterProduct{Tuple{taa_UX, }}
26+
top2= TagsOuterProduct{Tuple{taa_UXdot, taa_UXinv}}
27+
28+
@testset"TagsOuterProduct"begin
29+
@test top1[1]==u"m"
30+
@test top2[1,1]==u"s^-1"
31+
end
32+
33+
@testset"construct TaggedTensor"begin
34+
t1=TTensor(top1,
35+
@SArrayrand(2,)
36+
)
37+
38+
t2=TTensor(top2,
39+
@SArrayrand(2,2)
40+
)
41+
42+
t= t2* t1
43+
@test t.x== t2.x* t1.x
44+
@testget_tag(t)== TagsOuterProduct{Tuple{taa_UXdot}}
45+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp