Comments (5)
I'll address these in two comments!
In the current implementation, operators can combine with non-operators ("co-operators") using +
, -
and *
; I don't think I have the rules for this written down anywhere but I was careful to stick to the scmutils conventions and I don't think we can change them without getting into trouble mathematically.
Quick overview:
+ and -
When you add (or subtract) two operators, just like when you add two functions, you get a new operator that applies both operators and then adds (or subtracts) the two results:
emmy.env> (((+ D D) sin) 'x)
(* 2 (cos x))
When you add or subtract an operator with a non-operator, the non-operator is converted to an operator that composes the non-operator with the new operator's argument. (These two are covered by combine-op-f
and combine-f-op
in emmy.operator
, and the docstring describes what happens: https://github.com/mentat-collective/emmy/blob/main/src/emmy/operator.cljc#L229-L277)
emmy.env> (((+ D cos) sin) 'x)
(+
;; this first part is ((D sin) 'x)
(cos x)
;; the `cos` composed with `sin`, then the result applied to `x`
(cos (sin x)))
*
Multiplication of two operators is the same as composition. This is important, so that ((square D) f)
== (D (D f))
.
Multiplication of an operator by a non-operator works differently depending on if the operator is on the left or the right.
Op on the left
If the operator is on the left, Emmy handles (* <operator> <co-operator>)
by returning a new operator that does the following internally:
(fn [x]
(<operator> (* <co-operator> x)))
So your (* D sin)
example above becomes the operator (fn [x] (D (* sin x)))
, and the full example unfolds like this:
(((* D sin) 'x) 'y)
(((fn [x] (D (* sin x))) 'x) 'y)
((D (* sin 'x)) 'y)
;; remember now that (* sin 'x) is the same as `(fn [arg] (* (sin arg) ((constantly 'x) arg)))`
;; final result:
(* x (cos y))
Operator on the right
If the operator is on the right, THEN things work more like what you predicted, where (* <co-operator> <operator>)
becomes
(fn [x] (* <co-operator> (<operator> x)))
So this would fail with flipped args, because (D 'x)
is meaningless:
emmy.env> (((* sin D) 'x) 'y)
Execution error (IllegalArgumentException) at emmy.calculus.derivative/fn (derivative.cljc:459).
No method in multimethod 'partial-derivative' for dispatch value: [clojure.lang.Symbol clojure.lang.PersistentVector]
from emmy.
I tried for a while to get your version of the curl to work, but especially after reading around (e.g. https://math.stackexchange.com/questions/3999040/is-curl-actually-a-cross-product) I'm not convinced that the cross-product definition of curl can work... I don't think multiplication can make sense as application.
I do have this in emmy.calculus.vector-calculus
:
(def Curl
"Operator that takes a function `f` and returns a function that
calculates the [Curl](https://en.wikipedia.org/wiki/Curl_(mathematics)) of `f`
at its input point.
`f` must be a function from $\\mathbb{R}^3 \\to \\mathbb{R}^3$."
(-> (fn [f-triple]
(let [[Dx Dy Dz] (map d/partial [0 1 2])
fx (ref f-triple 0)
fy (ref f-triple 1)
fz (ref f-triple 2)]
(up (- (Dy fz) (Dz fy))
(- (Dz fx) (Dx fz))
(- (Dx fy) (Dy fx)))))
(o/make-operator 'Curl)))
which is equivalent to what you've got, just without the explicit cross-product written out.
Who knows, maybe Sussman has thoughts! Send him an email at [email protected], the question's just as applicable to scmutils and I'm curious to see what he thinks.
from emmy.
I'll close now but feel free to re-open, or continue the discussion on the closed issue...
from emmy.
Thanks Sam
The existence of your Curl function covers my immediate need, so I'm happy to leave the issue closed for now.
As for the rest of it, i.e. whether the cross-product notation is mathematically valid or just a useful hand-waving mnemonic, or simply only applicable to coords in ℝ³, I'm going to have to read up a bit more before I grasp completely.
Having read that math.stackexchange q, I suspect the latter.
Thanks for the pointers.
from emmy.
As for the (* <operator> <co-operator>)
explanation, OK, I can accept that ((* D f) g)
=> (D (* f g))
rather than ((D f) g)
, but I'll have to meditate a bit more to be happy with it.
from emmy.
Related Issues (20)
- Next Journal link leads to blank notebook HOT 1
- unary negation handling is broken in ->JavaScript HOT 3
- port functional version of partitions-M algorithm HOT 5
- Port axch's adaptive-plot library
- Links to documentation are broken HOT 2
- Feature request for 'collect' function
- Feature request for 'Expand' function
- Simplifier doesn't detect trigonometric identity HOT 1
- state-advancer throws "Cannot mix BigInt and other types" HOT 4
- CSE leaving a lot on the table due to variadic *, + etc HOT 1
- Overhaul SCI interface to use modern copy-ns etc HOT 1
- FDG Link in Readme broken HOT 1
- Add pretty-print extensions for custom types
- Protocol ICoordinateSystem is overwriting function uuid
- symbolic-taylor-series can't take perturbations as arguments
- Unexpected results from Brent minimizer. HOT 3
- Feature request: Plotting complex number functions
- Write a TeXmacs plugin
- Can `compare` handle literal square root? HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from emmy.