any util function for a Matrix X a float64?

27 views
Skip to first unread message

Letitgo

unread,
Jul 11, 2015, 8:31:28 PM7/11/15
to gonu...@googlegroups.com
otherwise, I have to do this:

// matrix m * float64 a
func mulMatrixVar(m *mat64.Dense, a float64) (*mat64.Dense) {
        mm, nn := m.Dims()
        arr := make([]float64, mm*nn)

        for i := 0; i < mm; i++ {
                for j := 0; j < nn; j++ {
                        arr[i*mm + j] = a * m.At(i, j)
                }
        }
        return mat64.NewDense(mm, nn, arr)
}

Dan Kortschak

unread,
Jul 11, 2015, 8:37:55 PM7/11/15
to Letitgo, gonu...@googlegroups.com
*mat64.Dense is a mat64.Scaler. If you want it in pace, just call Scale. If you want it not in place clone the matrix first.
--
You received this message because you are subscribed to the Google Groups "gonum-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gonum-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Letitgo

unread,
Jul 11, 2015, 8:53:28 PM7/11/15
to gonu...@googlegroups.com
thank you!

m.Scale(2, m)
it works.


On Saturday, July 11, 2015 at 5:37:55 PM UTC-7, kortschak wrote:
*mat64.Dense is a mat64.Scaler. If you want it in pace, just call Scale. If you want it not in place clone the matrix first.

On 12/07/2015, at 10:01 AM, "Letitgo" <mars....@gmail.com> wrote:

otherwise, I have to do this:

// matrix m * float64 a
func mulMatrixVar(m *mat64.Dense, a float64) (*mat64.Dense) {
        mm, nn := m.Dims()
        arr := make([]float64, mm*nn)

        for i := 0; i < mm; i++ {
                for j := 0; j < nn; j++ {
                        arr[i*mm + j] = a * m.At(i, j)
a typo here, it's i*nn+j
 

Dan Kortschak

unread,
Jul 11, 2015, 9:02:10 PM7/11/15
to Letitgo, gonu...@googlegroups.com
BTW, your original approach is not correct for all matrices since it assumes cols==stride.

Dan Kortschak

unread,
Jul 11, 2015, 9:04:15 PM7/11/15
to Dan Kortschak, Letitgo, gonu...@googlegroups.com
Also, my first claim about non-inplace is wrong - you can just use a zero matrix as the receiver and not do a clone.
Reply all
Reply to author
Forward
0 new messages