Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Springs [WIP] #1

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Springs [WIP] #1

wants to merge 5 commits into from

Conversation

adamhaber
Copy link
Collaborator

@adamhaber adamhaber commented Nov 26, 2020

Basic functionality for springs-regression animation.

Some todos:

  1. Fix the division-by-zero error
  2. ~~How to import all the functions already in better.clj? ~~
  3. Prettify the animation (once it's working :-))
  4. Add PCA animations (springs unconstrained on the rod's end)

@adamhaber adamhaber requested a review from sritchie December 8, 2020 20:44
Copy link
Member

@sritchie sritchie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!! I'd love to see a screenshot, but I'll also run this myself!

src/maturin/springs.clj Show resolved Hide resolved
(defn L-regression
[xs ys k]
(fn [[_ [slope intercept] _]]
(let [preds (map (linear-func slope intercept) xs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are definitely generalized coordinates :)

If you wanted to decompose this further, you could make a "spring" lagrangian, that takes, for position, a sequence of spring-stretches only... and then compose that with a transformation that does what you do internally, maps the

(defn line->offsets [xs ys]
  (fn [[slope intercept]]
    (let [preds (map (linear-func slope intercept) xs)]
      (mapv - preds ys))))

Then

(defn L-regression [xs ys k]
  (compose (L-springs k) (F->C (line->offsets xs ys))

should work! Just an idea...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented this, ran into a different problem, see comment below.

(q/background 100)
(q/fill color 255 255)
(let [[slope intercept] (convert state)]
(attach [0 intercept] [10 ((linear-func slope intercept) 10)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very cool. If you take my idea from above, about composing, I think you could use the exact same transformation to get positions for all of the points.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still can't get the graphics to work; the things I'm curious about (but can't experiment with at the moment) - if the drawing function only takes in the state - the intercept and the slope - how can I draw the xs and ys?

(up 1 1)
1)
built (build L initial-state)]
(q/defsketch regression
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to see it!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol me too :-)

@adamhaber
Copy link
Collaborator Author

Thanks for your comments Sam!

I've implemented

(defn L-regression-composed [xs ys k]
  (compose (L-springs k) (F->C (line->offsets xs ys))))

But now, when I call

(let [L (L-regression-composed x y 1)
      initial-state (up 1
                        (up 1 1)
                        1)
      built (build L initial-state)]
  (q/defsketch regression
    :title "Regression line"
    :size [500 500]
    :setup (:setup built)
    :update (:update built)
    :draw (draw-line (:xform built))
    :features [:keep-on-top]
    :middleware [m/fun-mode m/navigation-2d]))

I get:

Execution error (IllegalArgumentException) at sicmutils.generic/bin+ (generic.cljc:177).
No method in multimethod 'add' for dispatch value: [:sicmutils.structure/up :sicmutils.expression/numerical-expression]

When I use the original L-regression instead of the composed version (shouldn't they be identical?), I get the previous Division by zero error. Any ideas?

Comment on lines +52 to +53


Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea why Calva/cljfmt (my Clojure VSCode extension) forces the double spacing here? Same for the "fake data" comment below, but most comments don't have this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange, I do not!!

@sritchie
Copy link
Member

Hey, I'll load up your code in the next couple of days and give it a run myself to see what's up here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants