# Symbolics.jl substitute limitations

What are the limitations on the Symbolics.substitute command. I wanted to reduce the complexity of hand-code conversions in a symbolic inverse calculation by substituting the elements from the original calculation back into the final equations.

I’m hoping I’m not doing something terribly noobish in the way I’m using these functions.
The substitution command doesn’t seem to dive deep enough into the rather complicated equation to do the substitution. Am I asking too much of it?

Here is my Pluto Script.

``````### A Pluto.jl notebook ###
# v0.15.1

using Markdown
using InteractiveUtils

# ╔═╡ 24775b90-e4ce-11eb-1f12-c9bd98da675c
using Symbolics

# ╔═╡ 59c2fe10-72ab-4d72-a871-e01e4502129a
using LinearAlgebra

# ╔═╡ 1a732dc9-b494-44f8-a7b1-6849cce72980
using Latexify

# ╔═╡ 19d3033b-2c57-4aa0-88f7-da6c2d45dc67
using PlutoUI

# ╔═╡ 0165810f-bc22-45e7-b282-65bd8c98e93d

# ╔═╡ 2324e497-24e3-47f7-9b11-f1402776f6ab
@variables v[]

# ╔═╡ 5dba9a7e-cd13-4dcb-bc4f-2519d03b76b0
function conditionInputs(v)
[	v[6],
v[7],
v[6]*v[7],
]
end

# ╔═╡ 8368ff60-2e6a-435a-bc05-2d7d2cf9cef5
x=conditionInputs(v)

# ╔═╡ 1671d983-2fec-4589-aef4-0796993d6f98
latexify(x)

# ╔═╡ 05d2ecd4-1dfa-4b8a-abdc-067ef1df9092
begin
J=Symbolics.jacobian(conditionInputs(v),[v[6],v[7]])
latexify(J)
end

# ╔═╡ 93ee73f6-3a65-48b2-8da1-2810a40073d5
size(J)

# ╔═╡ e9c83264-0d20-4a26-9d1b-6a0d4684d929
@variables a[1:3]

# ╔═╡ 534581e7-f288-427f-ba15-68192e4c3c54
sensitivity=[a[1] a[2] a[3] ]'

# ╔═╡ a8be92ba-7743-4eba-8291-1820e6f0eccf
A=transpose(J)*J

# ╔═╡ 00fab7ac-0b91-4b3c-9ab2-c4bd689441ef
invA=inv(A)

# ╔═╡ ed5ea10a-1ea4-4fd7-8cab-d3368e37fa2b
Print(invA)

# ╔═╡ 9b10b854-2881-42a7-9dec-2332c32bf99b
invJ=invA*transpose(J)

# ╔═╡ 5ef5256d-3c84-40d3-807a-2984a8c14be4
size(invJ)

# ╔═╡ c57f9c7d-2089-4f24-b27e-d5a3c88d5dd9
conversionFunction=invJ*sensitivity

size(conversionFunction)

@variables A11, A21, A22, A31, A32, A33, test

# ╔═╡ cb64829b-9dda-40b7-bba8-b543ef0b71af
convDict=Dict((A[1] => A11),(A[1,2]=>A21),(A[2,2]=>A22),   )

# ╔═╡ acbdf3d5-2166-4b26-89a8-5b1d88c43927
invAsub = substitute(invA,convDict)

# ╔═╡ f11c4374-d556-44dc-a83d-812eedea060e
substitute.(A,(convDict  ,))

# ╔═╡ 0d02f5a3-e1b0-4f33-9757-bf5598e29459
conversionFunction2=substitute.(conversionFunction,(convDict  ,))

# ╔═╡ c2f2c09a-e19d-462c-a12f-d8babd3db28d
A

# ╔═╡ 4be4bc1e-8b5a-49e7-9a79-edfce960de81
Print(conversionFunction2)

# ╔═╡ fc676e40-772a-4969-ac59-8b0919487234

feval=Symbolics.build_function(conversionFunction2,[v,a]; target = Symbolics.MATLABTarget(), rhsnames=[:v_a])

# ╔═╡ e30d8a4d-b7e6-4862-8f4a-b80e4beda4f9
fevalA=Symbolics.build_function(A, [v]; target = Symbolics.MATLABTarget())

# ╔═╡ 1ecd5cf0-d51b-48d4-a1ec-db46557ed8d1
Print(feval)

# ╔═╡ 07eb1cb6-4a4b-4b46-a1dc-4bbfba74797f
Print(fevalA)

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[compat]
Latexify = "~0.15.6"
PlutoUI = "~0.7.9"
Symbolics = "~1.3.0"
"""

# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised

[[AbstractTrees]]
git-tree-sha1 = "03e0550477d86222521d254b741d470ba17ea0b5"
uuid = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
version = "0.3.4"

[[ArgTools]]

[[ArrayInterface]]
deps = ["IfElse", "LinearAlgebra", "Requires", "SparseArrays", "Static"]
git-tree-sha1 = "a71d224f61475b93c9e196e83c17c6ac4dedacfa"
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
version = "3.1.18"

[[Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[ChainRulesCore]]
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "f53ca8d41e4753c41cdafa6ec5f7ce914b34be54"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "0.10.13"

[[Combinatorics]]
git-tree-sha1 = "08c8b6831dc00bfea825826be0bc8336fc369860"
uuid = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
version = "1.0.2"

[[CommonSolve]]
uuid = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
version = "0.2.0"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "dc7dedc2c2aa9faf59a55c622760a25cbefbe941"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.31.0"

[[CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"

[[CompositeTypes]]
git-tree-sha1 = "d5b014b216dc891e81fea299638e4c10c657b582"
uuid = "b152e2b5-7a66-4b01-a709-34e65c35f657"
version = "0.1.2"

[[ConstructionBase]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "f74e9d5388b8620b4cee35d4c5a618dd4dc547f4"
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
version = "1.3.0"

[[DataAPI]]
git-tree-sha1 = "ee400abb2298bd13bfc3df1c412ed228061a2385"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.7.0"

[[DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.9"

[[DataValueInterfaces]]
git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6"
uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464"
version = "1.0.0"

[[Dates]]
deps = ["Printf"]

[[DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[DiffRules]]
deps = ["NaNMath", "Random", "SpecialFunctions"]
git-tree-sha1 = "85d2d9e2524da988bffaf2a381864e20d2dae08d"
uuid = "b552c78f-8df3-52c6-915a-8e097449b14b"
version = "1.2.1"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[Distributions]]
deps = ["FillArrays", "LinearAlgebra", "PDMats", "Printf", "QuadGK", "Random", "SparseArrays", "SpecialFunctions", "Statistics", "StatsBase", "StatsFuns"]
git-tree-sha1 = "3889f646423ce91dd1055a76317e9a1d3a23fff1"
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
version = "0.25.11"

[[DocStringExtensions]]
deps = ["LibGit2"]
git-tree-sha1 = "a32185f5428d3986f47c2ab78b1f216d5e6cc96f"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.8.5"

[[DomainSets]]
deps = ["CompositeTypes", "IntervalSets", "LinearAlgebra", "StaticArrays", "Statistics", "Test"]
git-tree-sha1 = "6cdd99d0b7b555f96f7cb05aa82067ee79e7aef4"
uuid = "5b8099bc-c8ec-5219-889f-1d9e522a28bf"
version = "0.5.2"

deps = ["ArgTools", "LibCURL", "NetworkOptions"]

[[DynamicPolynomials]]
deps = ["DataStructures", "Future", "LinearAlgebra", "MultivariatePolynomials", "MutableArithmetics", "Pkg", "Reexport", "Test"]
git-tree-sha1 = "5e47c4d652ea67652b7c5945c79c46472397d47f"
uuid = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
version = "0.3.18"

[[EllipsisNotation]]
deps = ["ArrayInterface"]
git-tree-sha1 = "8041575f021cba5a099a456b4163c9a08b566a02"
uuid = "da5c29d0-fa7d-589e-88eb-ea29b0a81949"
version = "1.1.0"

[[ExprTools]]
git-tree-sha1 = "b7e3d17636b348f005f11040025ae8c6f645fe92"
version = "0.1.6"

[[FillArrays]]
deps = ["LinearAlgebra", "Random", "SparseArrays"]
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.12.0"

[[Formatting]]
deps = ["Printf"]
git-tree-sha1 = "8339d61043228fdd3eb658d86c926cb282ae72a8"
uuid = "59287772-0a20-5a39-b81b-1366585eb4c0"
version = "0.4.2"

[[Future]]
deps = ["Random"]
uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820"

[[IfElse]]
git-tree-sha1 = "28e837ff3e7a6c3cdb252ce49fb412c8eb3caeef"
uuid = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
version = "0.1.0"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[IntervalSets]]
deps = ["Dates", "EllipsisNotation", "Statistics"]
git-tree-sha1 = "3cc368af3f110a767ac786560045dceddfc16758"
uuid = "8197267c-284f-5f27-9208-e0e47529a953"
version = "0.5.3"

[[IteratorInterfaceExtensions]]
git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856"
uuid = "82899510-4779-5014-852e-03e436cf321d"
version = "1.0.0"

[[JLLWrappers]]
deps = ["Preferences"]
git-tree-sha1 = "642a199af8b68253517b80bd3bfd17eb4e84df6e"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.3.0"

[[JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "81690084b6198a2e1da36fcfda16eeca9f9f24e4"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.1"

[[LaTeXStrings]]
git-tree-sha1 = "c7f1c695e06c01b95a67f0cd1d34994f3e7db104"
uuid = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
version = "1.2.1"

[[LabelledArrays]]
deps = ["ArrayInterface", "LinearAlgebra", "MacroTools", "StaticArrays"]
uuid = "2ee39098-c373-598a-b85f-a56591580800"
version = "1.6.2"

[[Latexify]]
deps = ["Formatting", "InteractiveUtils", "LaTeXStrings", "MacroTools", "Markdown", "Printf", "Requires"]
uuid = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
version = "0.15.6"

[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"

[[LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"

[[LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]

[[LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[LogExpFunctions]]
deps = ["DocStringExtensions", "LinearAlgebra"]
git-tree-sha1 = "7bd5f6565d80b6bf753738d2bc40a5dfea072070"
uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
version = "0.2.5"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[MacroTools]]
deps = ["Markdown", "Random"]
git-tree-sha1 = "6a8a2a625ab0dea913aba95c11370589e0239ff0"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.6"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[Missings]]
deps = ["DataAPI"]
git-tree-sha1 = "4ea90bd5d3985ae1f9a908bd4500ae88921c5ce7"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "1.0.0"

[[Mmap]]

[[MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"

[[MultivariatePolynomials]]
deps = ["DataStructures", "LinearAlgebra", "MutableArithmetics"]
git-tree-sha1 = "45c9940cec79dedcdccc73cc6dd09ea8b8ab142c"
uuid = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
version = "0.3.18"

[[MutableArithmetics]]
deps = ["LinearAlgebra", "SparseArrays", "Test"]
git-tree-sha1 = "3927848ccebcc165952dc0d9ac9aa274a87bfe01"
uuid = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
version = "0.2.20"

[[NaNMath]]
git-tree-sha1 = "bfe47e760d60b82b66b61d2d44128b62e3a369fb"
uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
version = "0.3.5"

[[NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[OpenSpecFun_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "13652491f6856acfd2db29360e1bbcd4565d04f1"
uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e"
version = "0.5.5+0"

[[OrderedCollections]]
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.4.1"

[[PDMats]]
deps = ["LinearAlgebra", "SparseArrays", "SuiteSparse"]
git-tree-sha1 = "4dd403333bcf0909341cfe57ec115152f937d7d8"
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
version = "0.11.1"

[[Parsers]]
deps = ["Dates"]
git-tree-sha1 = "c8abc88faa3f7a3950832ac5d6e690881590d6dc"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "1.1.0"

[[Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[PlutoUI]]
deps = ["Base64", "Dates", "InteractiveUtils", "JSON", "Logging", "Markdown", "Random", "Reexport", "Suppressor"]
git-tree-sha1 = "44e225d5837e2a2345e69a1d1e01ac2443ff9fcb"
uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
version = "0.7.9"

[[Preferences]]
deps = ["TOML"]
git-tree-sha1 = "00cfd92944ca9c760982747e9a1d0d5d86ab1e5a"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.2.2"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

deps = ["DataStructures", "LinearAlgebra"]
git-tree-sha1 = "12fbe86da16df6679be7521dfb39fbc861e1dc7b"
uuid = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
version = "2.4.1"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[RecipesBase]]
git-tree-sha1 = "b3fb709f3c97bfc6e948be68beeecb55a0b340ae"
uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
version = "1.1.1"

[[RecursiveArrayTools]]
deps = ["ArrayInterface", "ChainRulesCore", "DocStringExtensions", "LinearAlgebra", "RecipesBase", "Requires", "StaticArrays", "Statistics", "ZygoteRules"]
git-tree-sha1 = "2a76e8f24c67f3ebecaccefa8d4abd27db828407"
uuid = "731186ca-8d62-57ce-b412-fbd966d074cd"
version = "2.14.9"

[[Reexport]]
git-tree-sha1 = "5f6c21241f0f655da3952fd60aa18477cf96c220"
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
version = "1.1.0"

[[Requires]]
deps = ["UUIDs"]
git-tree-sha1 = "4036a3bd08ac7e968e27c203d45f5fff15020621"
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
version = "1.1.3"

[[Rmath]]
deps = ["Random", "Rmath_jll"]
uuid = "79098fc4-a85e-5d69-aa6a-4863f24498fa"
version = "0.7.0"

[[Rmath_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "68db32dff12bb6127bac73c209881191bf0efbb7"
uuid = "f50d1b31-88e8-58de-be2c-1cc44531875f"
version = "0.3.0+0"

[[RuntimeGeneratedFunctions]]
deps = ["ExprTools", "SHA", "Serialization"]
git-tree-sha1 = "5975a4f824533fa4240f40d86f1060b9fc80d7cc"
uuid = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
version = "0.5.2"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[SciMLBase]]
deps = ["ArrayInterface", "CommonSolve", "ConstructionBase", "Distributed", "DocStringExtensions", "IteratorInterfaceExtensions", "LinearAlgebra", "Logging", "RecipesBase", "RecursiveArrayTools", "StaticArrays", "Statistics", "Tables", "TreeViews"]
git-tree-sha1 = "932aaae93e81686e6473f27b5a11c403576a2183"
uuid = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
version = "1.18.0"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[Setfield]]
deps = ["ConstructionBase", "Future", "MacroTools", "Requires"]
git-tree-sha1 = "d5640fc570fb1b6c54512f0bd3853866bd298b3e"
uuid = "efcf1570-3423-57d1-acb7-fd33fddbac46"
version = "0.7.0"

[[SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SortingAlgorithms]]
deps = ["DataStructures"]
git-tree-sha1 = "b3363d7460f7d098ca0912c69b082f75625d7508"
uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
version = "1.0.1"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[SpecialFunctions]]
deps = ["ChainRulesCore", "LogExpFunctions", "OpenSpecFun_jll"]
git-tree-sha1 = "a50550fa3164a8c46747e62063b4d774ac1bcf49"
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
version = "1.5.1"

[[Static]]
deps = ["IfElse"]
git-tree-sha1 = "62701892d172a2fa41a1f829f66d2b0db94a9a63"
version = "0.3.0"

[[StaticArrays]]
deps = ["LinearAlgebra", "Random", "Statistics"]
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.2.7"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]

[[StatsAPI]]
git-tree-sha1 = "1958272568dc176a1d881acb797beb909c785510"
uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
version = "1.0.0"

[[StatsBase]]
deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics", "StatsAPI"]
git-tree-sha1 = "2f6792d523d7448bbe2fec99eca9218f06cc746d"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
version = "0.33.8"

[[StatsFuns]]
deps = ["LogExpFunctions", "Rmath", "SpecialFunctions"]
git-tree-sha1 = "30cd8c360c54081f806b1ee14d2eecbef3c04c49"
uuid = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
version = "0.9.8"

[[SuiteSparse]]
deps = ["Libdl", "LinearAlgebra", "Serialization", "SparseArrays"]
uuid = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"

[[Suppressor]]
git-tree-sha1 = "a819d77f31f83e5792a76081eee1ea6342ab8787"
uuid = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
version = "0.2.0"

[[SymbolicUtils]]
deps = ["AbstractTrees", "ChainRulesCore", "Combinatorics", "ConstructionBase", "DataStructures", "DynamicPolynomials", "IfElse", "LabelledArrays", "LinearAlgebra", "MultivariatePolynomials", "NaNMath", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArrays", "TimerOutputs"]
git-tree-sha1 = "17fecd52e9a82ca52bfc9d28a2d31b33458a6ce0"
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
version = "0.13.1"

[[Symbolics]]
deps = ["ConstructionBase", "DiffRules", "Distributions", "DocStringExtensions", "DomainSets", "IfElse", "Latexify", "Libdl", "LinearAlgebra", "MacroTools", "NaNMath", "RecipesBase", "Reexport", "Requires", "RuntimeGeneratedFunctions", "SciMLBase", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArrays", "SymbolicUtils", "TreeViews"]
git-tree-sha1 = "24fbb434af5bc33ab8df86857f0297fe8e4a1e90"
uuid = "0c5d862f-8b57-4792-8d23-62f2024744c7"
version = "1.3.0"

[[TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[[TableTraits]]
deps = ["IteratorInterfaceExtensions"]
git-tree-sha1 = "c06b2f539df1c6efa794486abfb6ed2022561a39"
uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
version = "1.0.1"

[[Tables]]
deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "TableTraits", "Test"]
git-tree-sha1 = "8ed4a3ea724dac32670b062be3ef1c1de6773ae8"
version = "1.4.4"

[[Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[TimerOutputs]]
deps = ["ExprTools", "Printf"]
git-tree-sha1 = "209a8326c4f955e2442c07b56029e88bb48299c7"
uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
version = "0.5.12"

[[TreeViews]]
deps = ["Test"]
git-tree-sha1 = "8d0d7a3fe2f30d6a7f833a5f19f7c7a5b396eae6"
uuid = "a2a6695c-b41b-5b7d-aed9-dbfdeacea5d7"
version = "0.3.0"

[[UUIDs]]
deps = ["Random", "SHA"]

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

[[ZygoteRules]]
deps = ["MacroTools"]
git-tree-sha1 = "9e7a1e8ca60b742e508a315c17eef5211e7fbfd7"
uuid = "700de1a5-db45-46bc-99cf-38207098b444"
version = "0.2.1"

[[nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

[[p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
"""

# ╔═╡ Cell order:
# ╠═0165810f-bc22-45e7-b282-65bd8c98e93d
# ╠═24775b90-e4ce-11eb-1f12-c9bd98da675c
# ╠═59c2fe10-72ab-4d72-a871-e01e4502129a
# ╠═1a732dc9-b494-44f8-a7b1-6849cce72980
# ╠═19d3033b-2c57-4aa0-88f7-da6c2d45dc67
# ╠═2324e497-24e3-47f7-9b11-f1402776f6ab
# ╠═5dba9a7e-cd13-4dcb-bc4f-2519d03b76b0
# ╠═8368ff60-2e6a-435a-bc05-2d7d2cf9cef5
# ╠═1671d983-2fec-4589-aef4-0796993d6f98
# ╠═05d2ecd4-1dfa-4b8a-abdc-067ef1df9092
# ╠═93ee73f6-3a65-48b2-8da1-2810a40073d5
# ╠═e9c83264-0d20-4a26-9d1b-6a0d4684d929
# ╠═534581e7-f288-427f-ba15-68192e4c3c54
# ╠═a8be92ba-7743-4eba-8291-1820e6f0eccf
# ╠═00fab7ac-0b91-4b3c-9ab2-c4bd689441ef
# ╠═cb64829b-9dda-40b7-bba8-b543ef0b71af
# ╠═acbdf3d5-2166-4b26-89a8-5b1d88c43927
# ╠═ed5ea10a-1ea4-4fd7-8cab-d3368e37fa2b
# ╠═9b10b854-2881-42a7-9dec-2332c32bf99b
# ╠═5ef5256d-3c84-40d3-807a-2984a8c14be4
# ╠═c57f9c7d-2089-4f24-b27e-d5a3c88d5dd9
# ╠═f11c4374-d556-44dc-a83d-812eedea060e
# ╠═0d02f5a3-e1b0-4f33-9757-bf5598e29459
# ╠═c2f2c09a-e19d-462c-a12f-d8babd3db28d
# ╠═4be4bc1e-8b5a-49e7-9a79-edfce960de81
# ╠═fc676e40-772a-4969-ac59-8b0919487234
# ╠═e30d8a4d-b7e6-4862-8f4a-b80e4beda4f9
# ╠═1ecd5cf0-d51b-48d4-a1ec-db46557ed8d1
# ╠═07eb1cb6-4a4b-4b46-a1dc-4bbfba74797f
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002

``````

It’s really hard to figure out the question from that post

I know. The question is. Why does substitution not work when I do this?

That’s what I’m saying, it’s hard to see what you’re trying to do. What’s the final equation, and the substitute expression?

Trying to substitute the dictionary convDict into the equation represented by conversionFunction2. This was a very adhoc script to see if I could dumb it down without the details.

``````@variables v[]
@variables a[1:3]
@variables A11, A21, A22, A31, A32, A33

convDict = Dict{Symbolics.Num, Symbolics.Num}(v[6]*v[7] => A21, 1 + v[6]^2 => A22, 1 + v[7]^2 => A11)
conversionFunction = Symbolics.Num[a[3]*(v[7]*((1 + v[7]^2)^-1)*(true + ((1 + v[6]^2 - ((v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)))^-1)*(v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)) - (v[7]*((1 + v[7]^2)^-1)*((1 + v[6]^2 - ((v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)))^-1)*(v[6]^2))) + a[1]*((1 + v[7]^2)^-1)*(true + ((1 + v[6]^2 - ((v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)))^-1)*(v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)) - (a[2]*v[6]*v[7]*((1 + v[7]^2)^-1)*((1 + v[6]^2 - ((v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)))^-1)); a[2]*((1 + v[6]^2 - ((v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)))^-1) + a[3]*(v[6]*((1 + v[6]^2 - ((v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)))^-1) - (v[6]*((1 + v[6]^2 - ((v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)))^-1)*(v[7]^2)*((1 + v[7]^2)^-1))) - (a[1]*v[6]*v[7]*((1 + v[7]^2)^-1)*((1 + v[6]^2 - ((v[6]^2)*(v[7]^2)*((1 + v[7]^2)^-1)))^-1))]

# I want to convert things like v[6]*v[7] in conversionFunction to A21 in conversionFunction2
conversionFunction2=substitute.(conversionFunction,(convDict  ,))

# Instead what I get is
Symbolics.Num[a[3]*((A11^-1)*v[7]*(1 + (A11^-1)*((1 + v[6]^2 - ((A11^-1)*(v[6]^2)*(v[7]^2)))^-1)*(v[6]^2)*(v[7]^2)) - ((A11^-1)*v[7]*((1 + v[6]^2 - ((A11^-1)*(v[6]^2)*(v[7]^2)))^-1)*(v[6]^2))) + (A11^-1)*a[1]*(1 + (A11^-1)*((1 + v[6]^2 - ((A11^-1)*(v[6]^2)*(v[7]^2)))^-1)*(v[6]^2)*(v[7]^2)) - ((A11^-1)*a[2]*v[6]*v[7]*((1 + v[6]^2 - ((A11^-1)*(v[6]^2)*(v[7]^2)))^-1)); a[2]*((1 + v[6]^2 - ((A11^-1)*(v[6]^2)*(v[7]^2)))^-1) + a[3]*(v[6]*((1 + v[6]^2 - ((A11^-1)*(v[6]^2)*(v[7]^2)))^-1) - ((A11^-1)*v[6]*((1 + v[6]^2 - ((A11^-1)*(v[6]^2)*(v[7]^2)))^-1)*(v[7]^2))) - ((A11^-1)*a[1]*v[6]*v[7]*((1 + v[6]^2 - ((A11^-1)*(v[6]^2)*(v[7]^2)))^-1))]

# It works for one or maybe 2 variables, depending on my equations, but not for all of them.
``````

Does that help? It’s hard to post Pluto output in graphical format.

Ultimately, I have a preconditioning function prior to training a neural network. I want to be able to backpropagate the errors all the way back to the inputs on that preconditioning function. I did it symbolically, because I need to implement it in C.

So having not done this manually before, I took pieces of that conditioning function that pertained to the inputs I am interested in, calculated the forward pass Jacobian, then calculated the pseudo inverse J^-1=inv(J’*J)*J’
Above I set A = (J’*J)
Then attempted to push back the sensitivities (a1) that I will have at the neural network back up to the pre-conditioned inputs (a0) by: a0=(J^-1) * a1.

I then took this “a0” which is called conversionFunction2 in this script and generated MATLAB code or C code from it. Since the intermediate value A’s elements, appears a lot in the symbolic output, I was trying to simplify the code generation by substituting the A’s back into the conversionFunction to make things a little more readable when the input vector grows to more than 3.

For expressions like that, you need to use replacement rules since that’s not a straight substitution.

Ok thanks. I’ll have to look up how to do that. I appreciate it.

In my quick look at SymbolicUtils.jl, I think it will be tricky to work with rules since the equations I want to replace are not generic equations with generic variables, but equalities specific to a variable. Maybe I could define a rule and then only call it with the specific variables I want to substitute. Maybe I could also convert the Symbolics to text using the build_function and then do a search and replace to swap out the text strings manually. For my case, I don’t need it to execute in Julia, just be easily understood.

I guess plan B is not to simplify it for readability and leave as is.

It seems like it works using string and replace. I just need to figure out how to go from a string back to a symbol afterwards.

You can just do that with rules. Make a rule based on the non-generic symbol, matching precisely the pieces you’re interested in.

I could not get that to work. I also only part of the items identified in the substitution dictionary would work, so it did some of them and then seemed to quit. The equation it generated was massive, so I was wondering if there was a recursion limit or something, but that still didn’t seem logical.

In the end, I generated code for my ultimate equation in it’s unreduced form. Then I went through my substitution matrix and generated code for each element individually and used a series of “replace” commands to trim it to just the equation since the beginning and end of that built function were generic. Then I generated my own code in text strings, to create the substitution variables and then searched and replaced them in the original string. Fairly non-elegant, but in the end produced prettier code than the native Julia build_function.

``````begin
editfile="% Autogenerated Substitution Sequence\n\n"
feval_simple = feval
for ii in [1 3 4 6 7 8 11 12 16]
default=Symbolics.build_function(A[ii],v;target=Symbolics.MATLABTarget())
thisString=replace(replace(default,"diffeqf = @(t,internal_var___u) [\n  "=>""),";\n];\n"=>"")
defString="    B\$ii = "*thisString*";\n"
editfile=editfile*defString
feval_simple = replace(feval_simple, thisString => "B\$ii")
end
Print(editfile)
end
``````

Then I cleaned it up for variables like “true” that appeared in the equation and giving it a proper header file before saving it to a file:

``````begin
# Clean up the function so it actually works in matlab
feval_final=replace(
replace(
replace(feval_simple,"diffeqf = @(t,v_a)"=>"function out=converter(v,a)\n\$editfile\n% Return Calculations\n\nout=")
,"//1 "=>" "),
"true"=>"1")

Print(feval_final)
end
``````

Don’t think anyone would ever want to do what I did, but leaving it here for a possible solution until what ever ails substitution is remedied. I’m not sure I would be very effective in debugging that.