如何将Clojure符号完全复制到新符号?

use*_*027 0 performance clojure mutable matrix immutability

我有代码直接改变矩阵的性能.在我改变它之前,我希望得到一个完整的副本来存储一个新符号,然后由变异过程使用.无论如何,我可以将Clojure符号的内容复制到一个新的符号中,以便第一个可以在不影响第二个的情况下进行变异吗?

这是我失败的尝试之一:

(var mat1 (clatrix/matrix (clatrix/ones 2 2)))
(var mat1)
(intern 'analyzer.core 'mat1 (clatrix/matrix (clatrix/ones 2 2)))
mat1
(intern 'analyzer.core 'mat2 mat1)
mat2
(clatrix/set mat1 0 0 2)
mat1
mat2
Run Code Online (Sandbox Code Playgroud)

当然,这不起作用:

(def mat1 (clatrix/matrix (clatrix/ones 2 2)
(def mat2 mat1)
Run Code Online (Sandbox Code Playgroud)

我也尝试过(但不确定我是否正好在这里做):

(def mat1 (clatrix/matrix (clatrix/ones 2 2)
(def mat2 `mat1)
Run Code Online (Sandbox Code Playgroud)

(def mat1 (clatrix/matrix (clatrix/ones 2 2))
(def mat2 ~mat1)
Run Code Online (Sandbox Code Playgroud)

(def mat1 (clatrix/matrix (clatrix/ones 2 2))
(def mat2 (.dup mat1))
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

更新

我对迄今为止提出的答案进行了基准测试.我不确定线条的符号是什么意思.建立:

(def mat1 (clatrix/ones 1000 1000) ; Creates a 1000x1000 matrix of 1.0 in each element.
Run Code Online (Sandbox Code Playgroud)

来自@Mars:

(criterium.core/bench (let [mat2 (clatrix/matrix mat1)]))
Run Code Online (Sandbox Code Playgroud)

benchmark1

来自@JoG:

(criterium.core/bench (let [mat2 (read-string (pr-str mat1))]))
Run Code Online (Sandbox Code Playgroud)

benchmark2

对于更一般的情况

@JoG的解决方案适用于串行化为字符串的数据结构.如果有人对如何制定更一般的解决方案有所了解,请回复,我会更新.

Mar*_*ars 5

只需使用matrix一次:

(require '[clatrix.core :as clatrix])
; nil

(def mat1 (clatrix/matrix [[1 1][1 1]]))
; #'user/mat1

(def mat2 (clatrix/matrix mat1))
; #'user/mat2

mat1
;  A 2x2 matrix
;  -------------
;  1.00e+00  1.00e+00 
;  1.00e+00  1.00e+00 

(clatrix/set mat1 0 0 2)
; #<DoubleMatrix [2.000000, 1.000000; 1.000000, 1.000000]>

mat1
;  A 2x2 matrix
;  -------------
;  2.00e+00  1.00e+00 
;  1.00e+00  1.00e+00 

mat2
;  A 2x2 matrix
;  -------------
;  1.00e+00  1.00e+00 
;  1.00e+00  1.00e+00 
Run Code Online (Sandbox Code Playgroud)