Hello
I've got a problem I'm stuck with and would value some extra input...
Basically I'd like to be able to define a maths function with a large
number of variables, then pass this and a map of arguments to a solve
function which solves for whatever is not defined in the map.
Now the solve function works well, using Apache Commons Math, but it
expects a function of one argument. However the only way I can think of
converting the argument list and the function into a function of one
argument is with an intermediate function. However approach is pretty
messy as I need to define two functions for each maths function and list
out the arguments twice.
Ideally I could pass the body of the maths function and a map of arguments
directly to the solve function without having to define a function
explicitly, and it would also work for multiple variables, but I might be
asking a lot.
Here is what I have:-
(defn -cat
"example math function"
[{:keys [E A alpha t2 t1 W1 W2 g L T1 T2]}]
(- (+ (* E A alpha (- t2 t1)) (/ (* (sq W1) (sq g) (sq L) E A) (* 24.0
(sq T1))) T2) T1 (/ (* (sq W2) (sq g) (sq L) E A) (* 24.0 (sq T2)))))
(defn cat
[m]
(let [v (difference #{:E :A :alpha :t2 :t1 :W1 :W2 :g :L :T1 :T2}
(keys m))]
(if-not (= (count v) 1)
(exception "incorrect number of variables"))
(fn [x] (-cat (assoc m (first v) x)))))
(solve (cat {:E 65000 :A 209.3 :alpha 0.000023 :t1 15 :t2 5 :W1 0.576 :W2
0.576 :g 9.81 :L 45 :T1 1595})
:start 1
:min 0
:max 100000
:max-eval 1000)
and the solver code is on github:
https://github.com/taprisiot/jlk.math/blob/master/src/jlk/math/optimization.clj
Now there has to be a better way of doing this but I'm stumped. Any ideas?
As a side note, being able to define functions in infix would also be nice,
but I don't feel up to creating a maths parser and dealing with ASTs,
although I can visualise how I might manage the variables using this
approach.
Thanks for any help!
- Lachlan
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en