Giter Club home page Giter Club logo

Comments (5)

sritchie avatar sritchie commented on July 17, 2024

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.

sritchie avatar sritchie commented on July 17, 2024

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.

sritchie avatar sritchie commented on July 17, 2024

I'll close now but feel free to re-open, or continue the discussion on the closed issue...

from emmy.

alexgian avatar alexgian commented on July 17, 2024

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.

alexgian avatar alexgian commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.