How do you multiply a matrix by a scalar in python?

Is there a more 'mathematical' way to do the following:

1.2738 * (list_of_items)

So for what I'm doing is:

[1.2738 * item for item in list_of_items]

asked Mar 2, 2015 at 23:40

David542David542

103k158 gold badges441 silver badges749 bronze badges

2

The mathematical equivalent of what you're describing is the operation of multiplication by a scalar for a vector. Thus, my suggestion would be to convert your list of elements into a "vector" and then multiply that by the scalar.

A standard way of doing that would be using numpy.

Instead of

1.2738 * (list_of_items)

You can use

import numpy
1.2738 * numpy.array(list_of_items)

Sample Output:

In [8]: list_of_items
Out[8]: [1, 2, 4, 5]

In [9]: import numpy

In [10]: 1.2738 * numpy.array(list_of_items)
Out[10]: array([ 1.2738,  2.5476,  5.0952,  6.369 ])

answered Mar 2, 2015 at 23:42

Another approach

map(lambda x:x*1.2738,list_of_items)

answered Mar 2, 2015 at 23:43

How do you multiply a matrix by a scalar in python?

levilevi

20.9k7 gold badges65 silver badges71 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a matrix and a scalar element k, our task is to find out the scalar product of that matrix. 
    Examples: 
     

    Input : mat[][] = {{2, 3}
                       {5, 4}}
            k = 5
    Output : 10 15 
             25 20 
    We multiply 5 with every element.
    
    Input : 1 2 3 
            4 5 6
            7 8 9
            k = 4
    Output :  4 8  12
              16 20 24
              28 32 36 

    The scalar multiplication of a number k(scalar), multiply it on every entry in the matrix. and a matrix A is the matrix kA.
     

    C++

    #include

    using namespace std;

    #define N 3

    void scalarProductMat(int mat[][N], int k)

    {

        for (int i = 0; i < N; i++)

            for (int j = 0; j < N; j++)

                mat[i][j] = mat[i][j] * k;       

    }

    int main()

    {

        int mat[N][N] = { { 1, 2, 3 },

                          { 4, 5, 6 },

                          { 7, 8, 9 } };

        int k = 4;

        scalarProductMat(mat, k);

        printf("Scalar Product Matrix is : \n");

        for (int i = 0; i < N; i++) {

            for (int j = 0; j < N; j++)

                printf("%d ", mat[i][j]);

            printf("\n");

        }

        return 0;

    }

    Java

    import java.io.*;

    class GFG {

    static final int N = 3;

    static void scalarProductMat(int mat[][],

                                      int k)

    {

        for (int i = 0; i < N; i++)

            for (int j = 0; j < N; j++)

                mat[i][j] = mat[i][j] * k;

    }

    public static void main (String[] args)

    {

        int mat[][] = { { 1, 2, 3 },

                        { 4, 5, 6 },

                        { 7, 8, 9 } };

        int k = 4;

        scalarProductMat(mat, k);

        System.out.println("Scalar Product Matrix is : ");

        for (int i = 0; i < N; i++)

        {

            for (int j = 0; j < N; j++)

                System.out.print(mat[i][j] + " ");

            System.out.println();

        }

    }

    }

    Python 3

    N = 3

    def scalarProductMat( mat, k):

        for i in range( N):

            for j in range( N):

                mat[i][j] = mat[i][j] * k    

    if __name__ == "__main__":

        mat = [[ 1, 2, 3 ],

               [ 4, 5, 6 ],

               [ 7, 8, 9 ]]

        k = 4

        scalarProductMat(mat, k)

        print("Scalar Product Matrix is : ")

        for i in range(N):

            for j in range(N):

                print(mat[i][j], end = " ")

            print()

    C#

    using System;

    class GFG{

    static int N = 3;

    static void scalarProductMat(int[,] mat,

                                      int k)

    {

        for (int i = 0; i < N; i++)

            for (int j = 0; j < N; j++)

                mat[i,j] = mat[i, j] * k;    

    }

    static public void Main ()

    {

        int[,] mat = {{1, 2, 3},

                      {4, 5, 6},

                      {7, 8, 9}};

        int k = 4;

        scalarProductMat(mat, k);

        Console.WriteLine("Scalar Product Matrix is : ");

        for (int i = 0; i < N; i++) {

            for (int j = 0; j < N; j++)

                Console.Write(mat[i, j] + " ");

            Console.WriteLine();

        }

    }

    }

    PHP

    function scalarProductMat($mat,

                              $k)

    {

        $N = 3;

        for ( $i = 0; $i < $N; $i++)

            for ($j = 0; $j < $N; $j++)

                $mat[$i][$j] = $mat[$i][$j] * $k;

        return $mat;

    }

    $N = 3;

    $mat = array(array(1, 2, 3 ),

                 array( 4, 5, 6 ),

                 array(7, 8, 9 ));

    $k = 4;

    $mat1 = scalarProductMat($mat, $k);

    echo("Scalar Product Matrix is : " . "\n");

    for ($i = 0; $i < $N; $i++)

    {

        for ($j = 0; $j < $N; $j++)

            echo($mat1[$i][$j] . " ");

        echo "\n";

    }

    Javascript

    Output:

    Scalar Product Matrix is : 
    4 8 12 
    16 20 24 
    28 32 36

    Time Complexity: O(n2),

    Auxiliary Space: O(1), since no extra space has been taken.


    How do you multiply a matrix by a number in Python?

    L L2=other. L result=[] if len(L[0])==len(L2): for i in range(len(L)): row=[] for j in range(len(L2[0])): var=0 for k in range(len(L2)): var=var+L[i][k]*L2[k][j] row=row+[var] result = result+[row] return matrix(result) else: raise ValueError('You may not only multiply m*n * n*q matrices.

    How do you multiply a list by a scalar in Python?

    Use the syntax [element * number for element in list] to multiply each element in list by number ..
    a_list = [1, 2, 3].
    multiplied_list = [element * 2 for element in a_list].
    print(multiplied_list).