Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 13 and Version 14 of QuickStart

Show
Ignore:
Author:
Anonymous (IP: 134.109.40.151)
Timestamp:
04/30/09 10:21:47 (15 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • QuickStart

    v13 v14  
    416416 
    417417== Operations == 
     418 
     419LYLA support many matrix operations.  For example, adding two matrices B and C and storing the result in a matrix A is done by 
     420{{{ 
     421#!d 
     422// A = B + C 
     423add(A, B, C); 
     424}}} 
     425As a general rule, the result of an operation is always stored in the '''first''' argument of the function.  Furthermore, for many operations it is possible to call the function as method call of the result matrix.  Therefore, the above example is equivalent to 
     426{{{ 
     427#!d 
     428// A = B + C 
     429A.add(B, C); 
     430}}} 
     431Many functions may store the result in one of the operands: 
     432{{{ 
     433#!d 
     434// A = A + B 
     435add(A, B); 
     436A.add(B); 
     437}}} 
     438The same is true for unary matrix functions, i.e. functions that work on a single matrix: 
     439{{{ 
     440#!d 
     441// A[i,j] = sqrt(B[i,j]) 
     442sqrt(A, B); 
     443A.sqrt(B); 
     444 
     445// A[i,j] = sqrt(A[i,j]); 
     446sqrt(A); 
     447A.sqrt(); 
     448}}} 
     449 
     450This table lists some of the binary operations for matrices: 
     451||{{{add(A,B,C)}}}||{{{A = B + C}}}|| 
     452||{{{sub(A,B,C)}}}||{{{A = B - C}}}|| 
     453||{{{div(A,B,C)}}}||{{{A[i,j] = B[i,j] / C[i,j]}}}|| 
     454||{{{pmul(A,B,C)}}}||{{{A[i,j] = B[i,j] * C[i,j]}}}|| 
     455||{{{mul(A,B,C)}}}||{{{A = B * C}}}|| 
     456 
     457This table lists some of the binary operations for matrices with scalars: 
     458||{{{add(A,x,C)}}}||{{{A[i,j] = x + C[i,j]}}}|| 
     459||{{{add(A,B,x)}}}||{{{A[i,j] = B[i,j] + x}}}|| 
     460||{{{sub(A,x,C)}}}||{{{A[i,j] = x - C[i,j]}}}|| 
     461||{{{sub(A,B,x)}}}||{{{A[i,j] = B[i,j] - x}}}|| 
     462||{{{div(A,x,C)}}}||{{{A[i,j] = x / C[i,j]}}}|| 
     463||{{{div(A,B,x)}}}||{{{A[i,j] = B[i,j] / x}}}|| 
     464||{{{pmul(A,x,C)}}}||{{{A[i,j] = x * C[i,j]}}}|| 
     465||{{{pmul(A,B,x)}}}||{{{A[i,j] = B[i,j] * x}}}|| 
     466 
     467Unary operations: 
     468||{{{abs(A,B)}}}||{{{A[i,j] = |B[i,j]|}}}|| 
     469||{{{neg(A,B)}}}||{{{A[i,j] = -B[i,j]}}}|| 
     470||{{{sqrt(A,B)}}}||{{{A[i,j] = sqrt(B[i,j])}}}|| 
     471||{{{sin(A,B)}}}||{{{A[i,j] = sin(B[i,j])}}}|| 
     472||{{{cos(A,B)}}}||{{{A[i,j] = cos(B[i,j])}}}|| 
     473 
     474=== Operators === 
     475Common matrix operators are overloaded: 
     476{{{#!d 
     477auto A = new DMatrix, B = new DMatrix; 
     478double x; 
     479... 
     480A += B;  // same as A.add(B); 
     481A += x;  // same as A.add(x); 
     482A -= B;  // same as A.sub(B); 
     483A -= x;  // same as A.sub(x); 
     484A *= B;  // same as A.mul(B); 
     485A *= x;  // same as A.mul(x); 
     486A /= B;  // same as A.div(B); 
     487A /= x;  // same as A.div(x); 
     488}}} 
     489 
     490Binary operators are overloaded, too, but there is one important difference to the explicit functions above.  Since operators return a value instead of modifying their arguments, calling for example {{{A + B}}} always creates a new matrix: 
     491{{{ 
     492#!d 
     493auto A = new DMatrix, B = new DMatrix; 
     494... 
     495auto C = A + B; // creates a new matrix C 
     496}}} 
     497The resulting matrix has usually the same orientation as the first operand and is sparse if and only if both operands are sparse.  This means, if you use only one matrix type, the result of the operations will always have the same type as both operands: 
     498{{{ 
     499#!d 
     500auto A = new DMatrix, B = new DMatrix; 
     501DMatrix C; 
     502... 
     503C = A * B; // works 
     504}}} 
     505 
     506=== BLAS like operations === 
     507LYLA support some BLAS-like functions.  If BLAS-support is compiled in, the arithmetic for dense matrices call the BLAS-functions to perform the operations.  Since the rule in LYLA is to store the result in the first argument (in contrast to BLAS where the result is usually stored in the last argument), must functions have another order of parameters than their BLAS counterparts: 
     508 
     509  * compute the inner product of two vectors {{{x}}} and {{{y}}} 
     510{{{ 
     511#!d 
     512value_t dot(Vector!(value_t) x, Vector!(value_t) y) 
     513}}} 
     514 
     515  * scale a vector y by factor alpha: x = x * alpha 
     516{{{ 
     517#!d 
     518void scal( Vector!(value_t) x, value_t alpha ) 
     519}}} 
     520 
     521  * compute x = x + alpha * y 
     522{{{ 
     523#!d 
     524void xepay( Vector!(value_t) x, value_t alpha, Vector!(value_t) y) 
     525}}} 
     526 
     527  * Multiply a matrix and a vector: x = x * alpha + A * y * beta 
     528{{{ 
     529#!d 
     530void vmul( Vector!(value_t) x, value_t alpha, Matrix!(value_t) A, Vector!(value_t) y, value_t beta ) 
     531}}} 
     532 
     533  * Multiply two matrices: A = A * alpha + B * C * beta 
     534{{{ 
     535#!d 
     536void mmul( Matrix!(value_t) A, value_t alpha, Matrix!(value_t) B, Matrix!(value_t) C, value_t beta ) 
     537}}} 
     538 
     539'''Remark:''' BLAS functions may be called as methods of the destination matrix, too: 
     540{{{ 
     541#!d 
     542/// A = A * alpha + B * C * beta 
     543A.mmul( alpha, B, C, beta ) 
     544}}} 
     545