これは、Finding integer power rootsの焼き直しですが、clojure です。
数値のすべての整数根を見つけるにはどうすればよいですか?
だから私は関数が欲しい:
(defn roots [ex num] .....)
呼び出されると、次のようになります。
(roots 4 81) => [3, -3, 3i, -3i]
これは、Finding integer power rootsの焼き直しですが、clojure です。
数値のすべての整数根を見つけるにはどうすればよいですか?
だから私は関数が欲しい:
(defn roots [ex num] .....)
呼び出されると、次のようになります。
(roots 4 81) => [3, -3, 3i, -3i]
私が数学のために見つけた最高のライブラリは、apache commons math ライブラリです
使用するには、これを project.clj に追加します
[org.apache.commons/commons-math3 "3.0"]
nth-root メソッドが組み込まれており、以下をラッパーにすることができます。
(org.apache.commons.math3.complex.Complex をインポート)
(複雑な定義
([re] (複雑な re 0))
([レイム]
(Complex/valueOf re im)))
(defn nth-root [#^Complex cm] (seq (.nthRoot cm)))
(defmethod 印刷方法
複雑
[このW]
(print (format "(%f %fi)" (re this) (im this))))
(n乗根 (複素数 1 4) 6)
;; => ((1.235513 0.277543i) (0.377397 1.208757i) (-0.858115 0.931214i) (-1.235513 -0.277543i) (-0.377397 -1.208757i) (0.858115 -0.931214i)
user> (defn nth-root
[n x]
(long (Math/pow x (/ 1.0 n))))
#'user/nth-root
user> (nth-root 4 81)
3
正直なところ、Clojure で標準化された複素数の処理方法がわかりません。Complexを使用して、独自のレコードを実装する必要がある場合がありますdefrecord。