# Broken BLAS.gemm with complex rectangular matrix

**URL:** <https://discourse.julialang.org/t/broken-blas-gemm-with-complex-rectangular-matrix/2247>\
**Category:** Numerics\
**Tags:** question, package, blas\
**Created:** [February 23, 2017, 9:48am UTC](https://discourse.julialang.org/t/broken-blas-gemm-with-complex-rectangular-matrix/2247 "2017-02-23T09:48:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcyang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcyang/32/638_2.png) [@dcyang](https://discourse.julialang.org/u/dcyang)\
**Post date:** [February 23, 2017, 9:48am UTC](https://discourse.julialang.org/t/broken-blas-gemm-with-complex-rectangular-matrix/2247/1 "2017-02-23T09:48:51Z")

</div>

Hi,  
I ran the following short code on two different versions of Julia and got results that are broken in different ways.

```julia-auto
import Base.LinAlg.BLAS.gemm

A = convert(Array{Complex64,2}, [-6im -5im; -4 3; 2im im])
println(size(A))

println("plain *")
println(transpose(A)*A)
println(ctranspose(A)*A)

println("blas gemm")
println(gemm('t','n',A,A))
println(gemm('c','n',A,A))

```

#### output from Julia 0.4.5 (provided as a Linux Mint package)

```julia-auto
(3,2)
plain *
Complex{Float32}[-24.0f0 + 0.0f0im -44.0f0 + 0.0f0im
                 -44.0f0 + 0.0f0im -17.0f0 + 0.0f0im]
Complex{Float32}[56.0f0 + 0.0f0im 20.0f0 + 0.0f0im
                 20.0f0 + 0.0f0im 35.0f0 + 0.0f0im]
blas gemm
Complex{Float32}[-24.0f0 + 0.0f0im -44.0f0 + 0.0f0im -5.835328f9 - 0.19085431f0im
                 -44.0f0 + 0.0f0im -17.0f0 + 0.0f0im 4.376496f9 - 0.095427155f0im]
Complex{Float32}[56.0f0 + 0.0f0im 20.0f0 + 0.0f0im -5.835328f9 + 0.19085431f0im
                 20.0f0 + 0.0f0im 35.0f0 + 0.0f0im 4.376496f9 + 0.095427155f0im]

```

#### output from nightly build 0.6.0-2a19c36283

```julia-auto
(3, 2)
plain *
Complex{Float32}[-24.0+0.0im -44.0+0.0im; -44.0+0.0im -17.0+0.0im]
Complex{Float32}[56.0+0.0im 20.0+0.0im; 20.0+0.0im 35.0+0.0im]
blas gemm
ERROR: LoadError: DimensionMismatch("A has size (2,3), B has size (2,3), C has size (2, 3)")
Stacktrace:
 [1] gemm!(::Char, ::Char, ::Complex{Float32}, ::Array{Complex{Float32},2}, ::Array{Complex{Float32},2}, ::Complex{Float32}, ::Array{Complex{Float32},2}) at ./linalg/blas.jl:1028
 [2] gemm(::Char, ::Char, ::Complex{Float32}, ::Array{Complex{Float32},2}, ::Array{Complex{Float32},2}) at ./linalg/blas.jl:1042
 [3] gemm(::Char, ::Char, ::Array{Complex{Float32},2}, ::Array{Complex{Float32},2}) at ./linalg/blas.jl:1045
 [4] include_from_node1(::String) at ./loading.jl:539
 [5] include(::String) at ./sysimg.jl:14
 [6] process_options(::Base.JLOptions) at ./client.jl:305
 [7] _start() at ./client.jl:371
while loading /home/me/src/jl/test-blas.jl, in expression starting on line 14

```

gemm works ok with the square matrices that I’ve tried so far.  
I think I reduced the problem to a point where all possible external causes have been ruled out.

Should I go ahead and open a GitHub issue?

Thanks.  
David

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [February 23, 2017, 3:02pm UTC](https://discourse.julialang.org/t/broken-blas-gemm-with-complex-rectangular-matrix/2247/2 "2017-02-23T15:02:27Z")

</div>

Use upper case flags ‘C’, ‘T’, ‘N’. gemm isn’t exported, so documentation is incomplete.

---

<div class="post-metadata">

**Author:** ![tkelman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkelman/32/692_2.png) [@tkelman](https://discourse.julialang.org/u/tkelman)\
**Post date:** [February 23, 2017, 5:55pm UTC](https://discourse.julialang.org/t/broken-blas-gemm-with-complex-rectangular-matrix/2247/3 "2017-02-23T17:55:58Z")

</div>

It should possibly be doing more input validation if that doesn’t harm performance.

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [February 23, 2017, 8:58pm UTC](https://discourse.julialang.org/t/broken-blas-gemm-with-complex-rectangular-matrix/2247/4 "2017-02-23T20:58:13Z")

</div>

Arguments are not allowed to alias in BLAS calls. That is a Fortran rule that allows for better compiler optimizations. See e.g. [Intel Developer Zone](https://software.intel.com/en-us/blogs/2009/07/10/doctor-fortran-in-ive-come-here-for-an-argument-side-2). The functions in the `BLAS` module are considered expert functions and should generally not be used directly. What is the reason you call them?

---

<div class="post-metadata">

**Author:** ![dcyang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcyang/32/638_2.png) [@dcyang](https://discourse.julialang.org/u/dcyang)\
**Post date:** [February 27, 2017, 2:23am UTC](https://discourse.julialang.org/t/broken-blas-gemm-with-complex-rectangular-matrix/2247/5 "2017-02-27T02:23:23Z")

</div>

Thanks for your replies.  
Using the upper case ‘C’, ‘T’ worked.

I was trying to do quantum mechanics with BLAS/LAPACK in the space of complex, highly rectangular m×n-matrices (n≫m~10).

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [February 27, 2017, 2:49am UTC](https://discourse.julialang.org/t/broken-blas-gemm-with-complex-rectangular-matrix/2247/6 "2017-02-27T02:49:45Z")

</div>

Notice that `*` calls BLAS.
