Skip to content

Commit

Permalink
More aggressive opt
Browse files Browse the repository at this point in the history
```julia
using MeshCat
using ModelingToolkit
using TORA
using RigidBodyDynamics
using Symbolics
import SymPy

function setup(vis, n_dof, sympy=false)
    robot = create_robot_kuka_iiwa_14(vis)
    x = MechanismState{sympy ? SymPy.Sym : Num}(robot.mechanism)
    q = configuration(x)
    if sympy
        for i in 1:n_dof
            q[i] = SymPy.symbols("q_$i", real = true)
        end
    else
        for i in 1:n_dof
            q[i] = Num(Sym{Real}(Symbol("q$i")))
        end
    end
    return x
end
vis = Visualizer()
```

Symbolics:
```julia
julia> x = setup(vis, 3); @time M = mass_matrix(x);
  3.721925 seconds (7.73 M allocations: 462.482 MiB, 2.41% gc time, 99.20% compilation time)

julia> for n in 3:7; x = setup(vis, n); @time M = mass_matrix(x); end
  0.006976 seconds (79.61 k allocations: 3.028 MiB)
  0.007024 seconds (80.71 k allocations: 3.072 MiB)
  0.007367 seconds (81.56 k allocations: 3.106 MiB)
  0.007341 seconds (82.07 k allocations: 3.131 MiB)
  0.007315 seconds (82.66 k allocations: 3.157 MiB)
```

SymPy:
```julia
julia> x = setup(vis, 3, true); @time M = mass_matrix(x);
 16.333147 seconds (31.71 M allocations: 1.870 GiB, 2.18% gc time)

julia> for n in 3:7; x = setup(vis, n, true); @time M = mass_matrix(x); end
  2.899745 seconds (78.61 k allocations: 2.414 MiB)
  4.737822 seconds (78.61 k allocations: 2.414 MiB)
  9.462235 seconds (78.61 k allocations: 2.414 MiB)
 16.858959 seconds (78.61 k allocations: 2.414 MiB)
 17.311054 seconds (78.61 k allocations: 2.414 MiB)
```
  • Loading branch information
YingboMa committed Mar 30, 2021
1 parent 525d38e commit cd74f56
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -735,29 +735,6 @@ end

Base.isequal(a::Mul, b::Mul) = isequal(a.coeff, b.coeff) && isequal(a.dict, b.dict)

Base.isequal(b::Add, a::Mul) = isequal(a, b)
function Base.isequal(a::Mul, b::Add) # this shows up a lot when merging dicts
# a = a.coeff * (a.key1 ^ a.val1) * ....
# b = b.coeff + (b.key1 * b.val1) + ...
# When `a == b`, the following criteria must satisfy
# (1) b.coeff == 0
# (2) a.coeff == b.val1
# (3) b.val1 == b.val2 == b.val3 == ...
# (4) a.val1 == a.val2 == a.val3 == 1...
# (5) a.keys \ b.keys == ∅

iszero(b.coeff) || return false # (1)
bv1, bvs = Iterators.peel(values(b.dict))
a.coeff == bv1 || return false # (2)
for bv in bvs # (3)
isequal(bv1, bv) || return false
end
for av in values(a.dict) # (4)
isone(av) || return false
end
isequal(keys(a.dict), keys(b.dict))
end

Base.show(io::IO, a::Mul) = show_term(io, a)

function makemul(coeff, xs...; d=sdict())
Expand Down Expand Up @@ -844,14 +821,6 @@ Base.hash(p::Pow, u::UInt) = hash(p.exp, hash(p.base, u))

Base.isequal(p::Pow, b::Pow) = isequal(p.base, b.base) && isequal(p.exp, b.exp)

Base.isequal(b::Mul, p::Pow) = isequal(p, b)
function Base.isequal(p::Pow, b::Mul)
isone(b.coeff) || return false
dict = b.dict
length(dict) == 1 || return false
isequal(p.exp, get(dict, p.base, nothing))
end

Base.show(io::IO, p::Pow) = show_term(io, p)

^(a::SN, b) = Pow(a, b)
Expand Down Expand Up @@ -963,3 +932,20 @@ function print_tree(_io::IO, x::Union{Term, Add, Mul, Pow}; kw...)
end
end
end

# These show up a lot when merging dicts
# TODO: prove these properties
Base.isequal(a::Add, b::Term) = false
Base.isequal(b::Term, a::Add) = isequal(a, b)
for T in [:Add, :Term]
@eval begin
Base.isequal(a::Mul, b::$T) = false
Base.isequal(b::$T, a::Mul) = isequal(a, b)
end
end
for T in [:Mul, :Add, :Term]
@eval begin
Base.isequal(a::Pow, b::$T) = false
Base.isequal(b::$T, a::Pow) = isequal(a, b)
end
end

0 comments on commit cd74f56

Please sign in to comment.