2014-08-29 46 views

回答

1

Google Apps腳本是Javascript的變體 - 所以,它可以支持矩陣操作或任何其他數學運算。也像Javascript一樣,它本身無法實現 - 您需要自己編寫函數,或者找到適合的函數庫。

對於特別是矩陣操作,這裏有一個選項。 Jos de Jong的mathjs用於Node.js的庫在Google Apps腳本中原樣使用。你可以閱讀它對矩陣here的支持。

  • 複製最小化math.jssource from github,並且將其粘貼到您想要的庫添加到腳本new script file。完成該操作後,該庫可作爲math訪問,例如, math.someMethod()

  • 試試下面的例子 - 評論表明您可以在日誌中會發生什麼:

/** 
    * Demonstrate mathjs array & matrix operations. 
    */ 
    function matrix_demo() { 
     var array = [[2, 0],[-1, 3]];    // Array 
     var matrix = math.matrix([[7, 1],[-2, 3]]); // Matrix 

     // perform a calculation on an array and matrix 
     print(math.square(array)); // Array, [[4, 0], [1, 9]] 
     print(math.square(matrix)); // Matrix, [[49, 1], [4, 9]] 

     // perform calculations with mixed array and matrix input 
     print(math.add(array, matrix)); // Matrix, [[9, 1], [-3, 6]] 
     print(math.multiply(array, matrix)); // Matrix, [[14, 2], [-13, 8]] 

     // create a matrix. Type of output of function ones is determined by the 
     // configuration option `matrix` 
     print(math.ones(2, 3)); // Matrix, [[1, 1, 1], [1, 1, 1]] 
    } 

    /** 
    * Helper function to output a value in the console. Value will be formatted. 
    * @param {*} value 
    */ 
    function print (value) { 
     var precision = 14; 
     Logger.log(math.format(value, precision)); 
    }