scala - Using the kronecker product on complex matrices with scalaNLP breeze -
i had piece of code:
def this(vectors: list[densevector[double]]) { this(vectors.length) var resultvector = vectors.head (vector <- vectors) { resultvector = kron(resultvector.todensematrix, vector.todensematrix).todensevector } _vector = resultvector }
it worked way wanted work. problem needed complex values in stead of doubles. after importing breeze.math.complex, changed code to:
def this(vectors: list[densevector[complex]]) { this(vectors.length) var resultvector = vectors.head (vector <- vectors) { resultvector = kron(resultvector.todensematrix, vector.todensematrix).todensevector } _vector = resultvector }
this results errors:
error:(42, 26) not find implicit value parameter impl: breeze.linalg.kron.impl2[breeze.linalg.densematrix[breeze.math.complex],breeze.linalg.densematrix[breeze.math.complex],vr] resultvector = kron(resultvector.todensematrix, vector.todensematrix).todensevector ^ error:(42, 26) not enough arguments method apply: (implicit impl: breeze.linalg.kron.impl2[breeze.linalg.densematrix[breeze.math.complex],breeze.linalg.densematrix[breeze.math.complex],vr])vr in trait ufunc. unspecified value parameter impl. resultvector = kron(resultvector.todensematrix, vector.todensematrix).todensevector ^
is bug or forgetting something?
i found problem in following way:
- i first rewrote function use less matrix conversions
- as there problem implicit
impl
variable of kron, rewrote function call explicitly state variable use use
.
def this(vectors: list[densevector[complex]]) { this(vectors.length) var resultmatrix = vectors.head.todensematrix (i <- 1 until vectors.length) { resultmatrix = kron(resultmatrix, vectors(i).todensematrix)(kron.krondm_m[complex, complex, densematrix[complex], complex]) } _vector = resultmatrix.todensevector }
this showed me there no scalarmulop
v2
, m
, densematrix[rv]
m
matrix[v1]
, v1
, v2
input types , rv
output type of scalarmulop
digging through source code of breeze found in densematrixops
there implicit scalarmulop
above types if v1
, v2
, rv
of type int
, long
, float
, double
. copying function , making specific complex numbers, able kronecker product work. remove explicit use of (kron.krondm_m[complex, complex, densematrix[complex], complex])
. scalarmulop
function in question is:
implicit def s_dm_op_complex_opmulscalar(implicit op: opmulscalar.impl2[complex, complex, complex]): opmulscalar.impl2[complex, densematrix[complex], densematrix[complex]] = new opmulscalar.impl2[complex, densematrix[complex], densematrix[complex]] { def apply(b: complex, a: densematrix[complex]): densematrix[complex] = { val res: densematrix[complex] = densematrix.zeros[complex](a.rows, a.cols) val resd: array[complex] = res.data val ad: array[complex] = a.data var c = 0 var off = 0 while (c < a.cols) { var r = 0 while (r < a.rows) { resd(off) = op(b, ad(a.linearindex(r, c))) r += 1 off += 1 } c += 1 } res } implicitly[binaryregistry[complex, matrix[complex], opmulscalar.type, matrix[complex]]].register(this) }
Comments
Post a Comment