Make complex arithmetic by default in a Clojure project

you have been very useful to me so far, I hope you can help me deal with this. I am building a project that performs calculations with complex matrices. I used mikera / core.matrix.complex which uses mikera / core.matrix for matrix material and complex.core for determining complex numbers in clojure.

I want to be able to do any arithmetic, including using irrational numbers such as e (def e (Math/E)) and pi (def pi (Math/PI)) , and include matrix math, which may or may not be complicated. The problem arises when I mix functions from different libraries.

Right now my namespace is as follows:

 (ns qgame.utils.math (:require [clojure.walk :as w :refer [postwalk prewalk]] [clojure.core.matrix :as mat] [clojure.core.matrix.complex :as compl] [incanter.core :as ic] [complex.core :as c :refer [+ - / *]])) 

If I do (mat/mmul matrix1 matrix2) , then everything is treated as a regular number, and using complex numbers will ruin it. I get a lot of ClassCastException org.apache.commons.math3.complex.Complex cannot be cast to java.lang.Number . If I just require core.matrix.complex without requiring core.matrix, then none of the matrix functions work.

The same goes for things like (+ 3 4) . If I directly (use 'complex.core) in repl, then this will be evaluated as (7.0, 0.0) , which is what I want, otherwise it will turn out as 7.

It seems to me that I am making it more complicated than that, core.matrix.complex looks like it has extensions for everything in core.matrix and clojure.core, but it does not use it with the namespace declaration that I have there is. I am not familiar with Clojure protocols, so I am definitely missing something. I just want all three libraries to be included, and that all math should be done in the context of complex numbers, i.e. 1 = (1.0, 0.0) and (Math/exp (* pi (complex 0 1)) => (-1.0, 0.0)

How can i do this? Sorry, if I am not clear, I will try to sort out any questions that I ask.

Edit: I was thinking about a problem, and actually the best place to start is the last equation I listed: Getting e ^ pi * I equal -1. If I try to use Math / exp with complex numbers, I get a casting exception.

+5
source share
1 answer

I am not familiar with core.matrix.complex, but here are some points that you probably don't know about. This is technically not an answer, but it is too long to capture a comment.

  • core.matrix defined a set of APIs in the form of protocols, which is defined here .

  • core.matrix.complex does not extend all protocols to complex matrices. For example, mat/mmul , defined here , depends on the PMatrixMultiply protocol. But this protocol does not apply to complex matrices.

  • (+ 3 4) returns (7.0, 0.0) after using complex.core , because complex.core rewrote the function / operator + . But I doubt that you should direct these operators. complex.core is just a thin wrapper around org.apache.commons.math3.complex.Complex and it knows nothing about matrices. You might be better off using functions / operators in the core.matrix API, for example add .

+1
source

Source: https://habr.com/ru/post/1244241/


All Articles