Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Product of row elements

    Dear all,

    I have a matrix E = ( 1, 2 , -3 ,-6 \ 3 ,4, 1, 3)

    How can I calculate the product of all element in each rows.

    The results should be r1 = 1 x 2 x (-3) x (-6) = 36 ; r2 = 3 x 4 x 1 x 3 = 36

    Thanks.

  • #2
    Some utility function may well exist: it's as quick to program it as a loop as to find out:

    Code:
    . mata
    ------------------------------------------------- mata (type end to exit) -----------------------
    : E = ( 1, 2 , -3 ,-6 \ 3 ,4, 1, 3)
    
    : E
            1    2    3    4
        +---------------------+
      1 |   1    2   -3   -6  |
      2 |   3    4    1    3  |
        +---------------------+
    
    : prod = E[,1]
    
    : for(j = 2; j<=cols(E); j++) {
    > prod = prod :* E[,j]
    > }
    
    : prod
            1
        +------+
      1 |  36  |
      2 |  36  |
        +------+

    Comment


    • #3
      Code:
      forvalues i = 1/2 {
          local product`i' = 1
          forvalues j = 1/4 {
              local product`i' = `product`i''*E[`i', `j']
          }
          display "Product for row `i' = `product`i''"
      }
      Added: crossed in cybersapce with Nick's response.

      Comment


      • #4
        Yet another way, avoiding the loop

        Code:
        mata :
        
        real matrix myrowproduct(real matrix x)
        {
            return((-1):^(mod(rowsum(x:<0), 2)):*exp(rowsum(ln(abs(x)))))
        }
        
        real matrix mycolproduct(real matrix x)
        {
            return(myrowproduct(x')')
        }
        
        E = ( 1, 2 , -3 ,-6 \ 3 ,4, 1, 3)
        E
        myrowproduct(E)
        mycolproduct(E)
        
        end
        gives

        Code:
        . mata :
        ------------------------------------------------- mata (type end to exit) -------------------------------------------------------------------------------------------
        : 
        : real matrix myrowproduct(real matrix x)
        > {
        >         return((-1):^(mod(rowsum(x:<0), 2)):*exp(rowsum(ln(abs(x)))))
        > }
        
        : 
        : real matrix mycolproduct(real matrix x)
        > {
        >         return(myrowproduct(x')')
        > }
        
        : 
        : E = ( 1, 2 , -3 ,-6 \ 3 ,4, 1, 3)
        
        : E
                1    2    3    4
            +---------------------+
          1 |   1    2   -3   -6  |
          2 |   3    4    1    3  |
            +---------------------+
        
        : myrowproduct(E)
                1
            +------+
          1 |  36  |
          2 |  36  |
            +------+
        
        : mycolproduct(E)
                 1     2     3     4
            +-------------------------+
          1 |    3     8    -3   -18  |
            +-------------------------+
        
        : 
        : end
        Anyone a better way to adjust for the sign?

        Best
        Daniel

        Comment


        • #5
          Also you can convert your matrix to variables
          Then you can use varprod user written command

          https://ideas.repec.org/c/boc/bocode/s457410.html
          Emad A. Shehata
          Professor (PhD Economics)
          Agricultural Research Center - Agricultural Economics Research Institute - Egypt
          Email: [email protected]
          IDEAS: http://ideas.repec.org/f/psh494.html
          EconPapers: http://econpapers.repec.org/RAS/psh494.htm
          Google Scholar: http://scholar.google.com/citations?...r=cOXvc94AAAAJ

          Comment


          • #6
            If the problem is row product over variables, then Phil Ryan already published this in 2001:

            STB-60 dm87 . . . . . . . . . . Calculating the row product of observations
            (help rprod if installed) . . . . . . . . . . . . . . . . . . P. Ryan
            3/01 pp.3--4; STB Reprints Vol 10, pp.39--41
            generates new variable whose values are the row product of
            observations

            Comment


            • #7
              To add more detail, Phil Ryan's code can be installed by clicking on the appropriate link after

              Code:
               
              net describe dm87, from(http://www.stata.com/stb/stb60)
              after which

              Code:
              help rprod
              explains that it is an add-on egen function.

              Comment


              • #8
                You can have a one-line solution without a user-defined function if you wish.
                For non-negative numbers we can utilize the properties of logarithms, i.e. log(a*b) = log(a) + log(b):
                Code:
                P = (p = exp(sum(log(E), 1))) < . ? p : 0
                where P is the product, p is an ancillary real scalar (to make one-line expression), E is a real rowvector, real colvector, real matrix, etc.

                For any number, fingers crossed :-),
                Code:
                P = (p = Re(exp(sum(log(C(E)), 1)))) < . ? p : 0
                since the logarithm of negative values exists in complex plane
                where P is the product, p is an ancillary real scalar (to make one-line expression), E is a complex rowvector, complex colvector, complex matrix, etc.

                PS log(0) = . in both real and complex planes and . * whatever = ., hence we need to check for this variant explicitly.
                PSS the second argument in sum() changes treatment of missing values, forcing sum() not to skip them
                Last edited by Ilya Bolotov; 20 Jan 2022, 16:53.

                Comment

                Working...
                X