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

Eliminate non-singularity checks in lattice code #5

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/lattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
abstract type AbstractLattice{T} <: AbstractMatrix{T} end
struct Lattice{T} <: AbstractLattice{T}
data::MMatrix{3,3,T,9}
function Lattice{T}(data) where {T}
@assert !iszero(det3x3(data)) "lattice is singular!"
return new(data)
end
end
"""
Lattice(data::AbstractMatrix)
Expand All @@ -24,7 +20,7 @@
The basis vectors of the matrix are stored as columns.

# Examples
```jldoctest

Check failure on line 23 in src/lattice.jl

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in ~/work/CrystallographyCore.jl/CrystallographyCore.jl/src/lattice.jl:23-33 ```jldoctest julia> Lattice([ 1.2 4.5 7.8 2.3 5.6 8.9 3.4 6.7 9.1 ]) 3×3 Lattice{Float64} 1.2 4.5 7.8 2.3 5.6 8.9 3.4 6.7 9.1 ``` Subexpression: Lattice([ Evaluated output: ERROR: syntax: incomplete: premature end of input Stacktrace: [1] top-level scope @ none:1 Expected output: 1.2 4.5 7.8 2.3 5.6 8.9 3.4 6.7 9.1 ]) 3×3 Lattice{Float64} 1.2 4.5 7.8 2.3 5.6 8.9 3.4 6.7 9.1 diff = Warning: Diff output requires color. 1.2 4.5 7.8 2.3 5.6 8.9 3.4 6.7 9.1 ]) 3×3 Lattice{Float64} 1.2 4.5 7.8 2.3 5.6 8.9 3.4 6.7 9.1ERROR: syntax: incomplete: premature end of input Stacktrace: [1] top-level scope @ none:1
julia> Lattice([
1.2 4.5 7.8
2.3 5.6 8.9
Expand Down Expand Up @@ -60,7 +56,7 @@
Construct a `Lattice` from, e.g., a vector of three basis vectors.

# Examples
```jldoctest

Check failure on line 59 in src/lattice.jl

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in ~/work/CrystallographyCore.jl/CrystallographyCore.jl/src/lattice.jl:59-95 ```jldoctest julia> Lattice([[1.2, 2.3, 3.4], [4.5, 5.6, 6.7], [7.8, 8.9, 9.10]]) 3×3 Lattice{Float64} 1.2 4.5 7.8 2.3 5.6 8.9 3.4 6.7 9.1 julia> Lattice(((1.1, 2.2, 3.1), (4.4, 5.5, 6.5), (7.3, 8.8, 9.9))) 3×3 Lattice{Float64} 1.1 4.4 7.3 2.2 5.5 8.8 3.1 6.5 9.9 julia> Lattice((1.1, 2.2, 3.1, 4.4, 5.5, 6.5, 7.3, 8.8, 9.9)) 3×3 Lattice{Float64} 1.1 4.4 7.3 2.2 5.5 8.8 3.1 6.5 9.9 julia> Lattice(i * 1.1 for i in 1:9) 3×3 Lattice{Float64} 1.1 4.4 7.700000000000001 2.2 5.5 8.8 3.3000000000000003 6.6000000000000005 9.9 julia> using Unitful, UnitfulAtomic julia> Lattice([ [4u"nm", 0u"m", 0.0u"cm"], [0u"cm", 180.0u"bohr", 0u"m"], [0u"bohr", 0u"nm", (3//1) * u"angstrom"], ]) 3×3 Lattice{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}} 4.0e-9 m 0.0 m 0.0 m 0.0 m 9.525189796254e-9 m 0.0 m 0.0 m 0.0 m 3.0e-10 m ``` Subexpression: Lattice([ Evaluated output: ERROR: syntax: incomplete: premature end of input Stacktrace: [1] top-level scope @ none:1 Expected output: [4u"nm", 0u"m", 0.0u"cm"], [0u"cm", 180.0u"bohr", 0u"m"], [0u"bohr", 0u"nm", (3//1) * u"angstrom"], ]) 3×3 Lattice{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}} 4.0e-9 m 0.0 m 0.0 m 0.0 m 9.525189796254e-9 m 0.0 m 0.0 m 0.0 m 3.0e-10 m diff = Warning: Diff output requires color. [4u"nm", 0u"m", 0.0u"cm"], [0u"cm", 180.0u"bohr", 0u"m"], [0u"bohr", 0u"nm", (3//1) * u"angstrom"], ]) 3×3 Lattice{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}} 4.0e-9 m 0.0 m 0.0 m 0.0 m 9.525189796254e-9 m 0.0 m 0.0 m 0.0 m 3.0e-10 mERROR: syntax: incomplete: premature end of input Stacktrace: [1] top-level scope @ none:1
julia> Lattice([[1.2, 2.3, 3.4], [4.5, 5.6, 6.7], [7.8, 8.9, 9.10]])
3×3 Lattice{Float64}
1.2 4.5 7.8
Expand Down Expand Up @@ -130,9 +126,3 @@
bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{Lattice}}, ::Type{S}
) where {S} = similar(Lattice{S}, axes(bc))
Lattice{S}(::UndefInitializer, dims) where {S} = Lattice(Array{S,length(dims)}(undef, dims))

# `LinearAlgebra.det` is much slower and more inaccurate than my simple `det3x3`.
function det3x3(matrix)
a, d, g, b, e, h, c, f, i = matrix # Only works for 3×3 matrices
return a * e * i + b * f * g + c * d * h - c * e * g - b * d * i - a * f * h
end
6 changes: 0 additions & 6 deletions test/lattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ using Unitful, UnitfulAtomic
3.4 6.7 9.1
]
@test Lattice(mat) == Lattice(MMatrix{3,3}(mat))
# Singular matrix
@test_throws AssertionError Lattice([
1 2 3
4 5 6
7 8 9
])
# Rectangular matrix
@test_throws DimensionMismatch Lattice([
1 2
Expand Down
Loading