mikera / vectorz-clj

Fast matrix and vector maths library for Clojure - as a core.matrix implementation
203 stars 19 forks source link

cast mikera.vectorz.Vector to clojure.lang.PersistentVector #50

Closed fyquah closed 9 years ago

fyquah commented 9 years ago

Is there anyway to do the above typecast ?

I am trying to use incanter.charts/scatter-plot

The following works fine:

(incanter.charts/scatter-plot [1 2 3] [4 5 6])

But this:

(require [clojure.core.matrix :as m])
(m/set-current-implementation :vectorz)

(def x (m/matrix (for [x (range 10)] (+ x (* 0.1 (rand))))))
(def y (m/matrix (for [x (range 10)] (+ x (* 0.1 (rand))))))
(incanter.charts/scatter-plot x y)

throws the following error:

Exception in thread "main" java.lang.ClassCastException: mikera.vectorz.Vector cannot be cast to java.lang.Number, compiling:(/tmp/form-init6889720767473931280.clj:1:72)
        at clojure.lang.Compiler.load(Compiler.java:7142)
        at clojure.lang.Compiler.loadFile(Compiler.java:7086)
        at clojure.main$load_script.invoke(main.clj:274)
        at clojure.main$init_opt.invoke(main.clj:279)
        at clojure.main$initialize.invoke(main.clj:307)
        at clojure.main$null_opt.invoke(main.clj:342)
        at clojure.main$main.doInvoke(main.clj:420)
        at clojure.lang.RestFn.invoke(RestFn.java:421)
        at clojure.lang.Var.invoke(Var.java:383)
        at clojure.lang.AFn.applyToHelper(AFn.java:156)
        at clojure.lang.Var.applyTo(Var.java:700)
        at clojure.main.main(main.java:37)
Caused by: java.lang.ClassCastException: mikera.vectorz.Vector cannot be cast to java.lang.Number
        at clojure.lang.RT.doubleCast(RT.java:1222)
        at incanter.charts$scatter_plot_STAR_$fn__13382.invoke(charts.clj:1365)
        at clojure.core$map$fn__4249.invoke(core.clj:2564)
        at clojure.lang.LazySeq.sval(LazySeq.java:40)
        at clojure.lang.LazySeq.seq(LazySeq.java:49)
        at clojure.lang.RT.seq(RT.java:484)
        at clojure.core$seq.invoke(core.clj:133)
        at clojure.core$dorun.invoke(core.clj:2855)
        at incanter.charts$scatter_plot_STAR_.doInvoke(charts.clj:1363)
        at clojure.lang.RestFn.applyTo(RestFn.java:142)
        at clojure.core$apply.invoke(core.clj:624)
        at clj_lin_reg.core$_main.invoke(core.clj:13)
        at clojure.lang.Var.invoke(Var.java:375)
        at user$eval5.invoke(form-init6889720767473931280.clj:1)
        at clojure.lang.Compiler.eval(Compiler.java:6703)
        at clojure.lang.Compiler.eval(Compiler.java:6693)
        at clojure.lang.Compiler.load(Compiler.java:7130)
        ... 11 more

What I had in mind was to cast it to a persistent vector before transforming it. Is there an API for this library to do that?

mikera commented 9 years ago

It is best to use clojure.core.matrix/to-nested-vectors.

Ideally Incanter would do this itself as part of the scatter-plot function. This will work with any core.matrix implementation. Perhaps worth raising the issue for Incanter?

mikera commented 9 years ago

Closing this because I think the question is answered and it is not really a vectorz-clj issue