Skip to content

Commit bb55cb5

Browse files
committed
format
1 parent 6b60b1a commit bb55cb5

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

src/input_affine_form.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ are nonlinear in the inputs, the result may not be meaningful.
4343
"""
4444
function input_affine_form(eqs, inputs)
4545
# Extract the input matrix g(x) by taking coefficients of each input
46-
g = [Symbolics.coeff(Symbolics.simplify(eq, expand=true), u) for eq in eqs, u in inputs]
47-
g = Symbolics.simplify.(g, expand=true)
48-
46+
g = [Symbolics.coeff(Symbolics.simplify(eq, expand = true), u)
47+
for eq in eqs, u in inputs]
48+
g = Symbolics.simplify.(g, expand = true)
49+
4950
# Extract the drift term f(x) by substituting inputs = 0
5051
f = Symbolics.substitute.(eqs, Ref(Dict(inputs .=> 0)))
51-
f = Symbolics.simplify.(f, expand=true)
52-
52+
f = Symbolics.simplify.(f, expand = true)
53+
5354
return f, g
54-
end
55+
end

test/input_affine_form.jl

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ using StaticArrays
88
@variables x1 x2 u1 u2
99
state = [x1, x2]
1010
inputs = [u1, u2]
11-
11+
1212
eqs = [
1313
-x1 + 2*x2 + u1,
1414
x1*x2 - x2 + u1 + 2*u2
1515
]
16-
16+
1717
f, g = input_affine_form(eqs, inputs)
18-
18+
1919
# Verify reconstruction
2020
eqs_reconstructed = f + g * inputs
2121
@test isequal(Symbolics.simplify.(eqs_reconstructed), Symbolics.simplify.(eqs))
22-
22+
2323
# Check dimensions
2424
@test length(f) == length(eqs)
2525
@test size(g) == (length(eqs), length(inputs))
@@ -45,19 +45,19 @@ using StaticArrays
4545
function H(q, q̇)
4646
return SA[
4747
-m * L * sin(q[2]) * q̇[2] + bt * (q̇[1] - R * q̇[2]) / R,
48-
-m * grav * L * sin(q[2]) - bt * (q̇[1] - R * q̇[2]),
48+
-m * grav * L * sin(q[2]) - bt * (q̇[1] - R * q̇[2])
4949
]
5050
end
5151
B(q) = SA[Km / R, -Km]
5252

5353
# Convert to control affine form
5454
function f_seg(x)
55-
q, q̇ = x[SA[1,2]], x[SA[3,4]]
55+
q, q̇ = x[SA[1, 2]], x[SA[3, 4]]
5656
return [q̇; -D(q) \ H(q, q̇)]
5757
end
5858
function g_seg(x)
59-
q, q̇ = x[SA[1,2]], x[SA[3,4]]
60-
return [SA[0,0]; D(q) \ B(q)]
59+
q, q̇ = x[SA[1, 2]], x[SA[3, 4]]
60+
return [SA[0, 0]; D(q) \ B(q)]
6161
end
6262

6363
# Trace dynamics symbolically
@@ -68,17 +68,17 @@ using StaticArrays
6868

6969
# Extract control-affine form
7070
fe, ge = input_affine_form(eqs, inputs)
71-
71+
7272
# Test reconstruction
7373
eqs2 = fe + ge * inputs
74-
diff = Symbolics.simplify.(eqs2 - eqs, expand=true)
75-
74+
diff = Symbolics.simplify.(eqs2 - eqs, expand = true)
75+
7676
# The difference should be zero or very close to zero symbolically
7777
# We test numerically since symbolic simplification might not be perfect
78-
f2, _ = build_function(fe, x, expression=false)
79-
g2, _ = build_function(ge, x, expression=false)
80-
81-
for i = 1:10
78+
f2, _ = build_function(fe, x, expression = false)
79+
g2, _ = build_function(ge, x, expression = false)
80+
81+
for i in 1:10
8282
x_val = rand(length(x))
8383
@test f2(x_val) f_seg(x_val) rtol=1e-10
8484
@test g2(x_val) g_seg(x_val) rtol=1e-10
@@ -90,24 +90,25 @@ using StaticArrays
9090
@variables x1 x2 x3 u1 u2
9191
state = [x1, x2, x3]
9292
inputs = [u1, u2]
93-
93+
9494
eqs = [
9595
x2,
9696
x3,
9797
-x1 - 2*x2 - x3 + u1 + 3*u2
9898
]
99-
99+
100100
f, g = input_affine_form(eqs, inputs)
101-
101+
102102
# Expected results
103103
f_expected = [x2, x3, -x1 - 2*x2 - x3]
104104
g_expected = [0 0; 0 0; 1 3]
105-
105+
106106
@test isequal(Symbolics.simplify.(f), Symbolics.simplify.(f_expected))
107-
107+
108108
# Test g matrix elements
109109
for i in 1:size(g, 1), j in 1:size(g, 2)
110-
@test isequal(Symbolics.simplify(g[i,j]), g_expected[i,j])
110+
111+
@test isequal(Symbolics.simplify(g[i, j]), g_expected[i, j])
111112
end
112113
end
113114

@@ -116,20 +117,19 @@ using StaticArrays
116117
@variables x1 x2 u
117118
state = [x1, x2]
118119
inputs = [u]
119-
120+
120121
eqs = [
121122
x2,
122123
-sin(x1) - x2 + u
123124
]
124-
125+
125126
f, g = input_affine_form(eqs, inputs)
126-
127+
127128
# Expected results
128129
f_expected = [x2, -sin(x1) - x2]
129130
g_expected = reshape([0, 1], 2, 1)
130-
131+
131132
@test isequal(Symbolics.simplify.(f), Symbolics.simplify.(f_expected))
132133
@test isequal(g, g_expected)
133134
end
134-
135-
end
135+
end

0 commit comments

Comments
 (0)