2021-02-01 21:31:45 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								/* ******************************************************************************
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								 * 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  This  program  and  the  accompanying  materials  are  made  available  under  the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  terms  of  the  Apache  License ,  Version  2.0  which  is  available  at 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  https : //www.apache.org/licenses/LICENSE-2.0.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * 
							 
						 
					
						
							
								
									
										
										
										
											2021-02-01 21:31:45 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 *   See  the  NOTICE  file  distributed  with  this  work  for  additional 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *   information  regarding  copyright  ownership . 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								 *  Unless  required  by  applicable  law  or  agreed  to  in  writing ,  software 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  distributed  under  the  License  is  distributed  on  an  " AS IS "  BASIS ,  WITHOUT 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  WARRANTIES  OR  CONDITIONS  OF  ANY  KIND ,  either  express  or  implied .  See  the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  License  for  the  specific  language  governing  permissions  and  limitations 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  under  the  License . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  SPDX - License - Identifier :  Apache - 2.0 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Created by Yurii Shyrma on 18.12.2017.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# ifndef LIBND4J_HOUSEHOLDER_H 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# define LIBND4J_HOUSEHOLDER_H 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-02 12:49:41 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  "array/NDArray.h" 
  
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-02 12:49:41 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								namespace  sd  {  
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								namespace  ops  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								namespace  helpers  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								template  < typename  T >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class  Householder  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public : 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    /**
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   this  method  calculates  Householder  matrix  P  =  identity_matrix  -  coeff  *  w  *  w ^ T 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   P  *  x  =  [ normX ,  0 ,  0  ,  0 ,  . . . ] 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    *   coeff  -  scalar 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    *   w  =  [ 1 ,  w1 ,  w2 ,  w3 ,  . . . ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   w  =  u  /  u0 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   u  =  x  -  | x | * e0 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    *   u0  =  x0  -  | x | 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    *   e0  =  [ 1 ,  0 ,  0  ,  0 ,  . . . ] 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    * 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    *   x  -  input  vector ,  remains  unaffected 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // static NDArray evalHHmatrix(const NDArray& x);
 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /**
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   this  method  evaluates  data  required  for  calculation  of  Householder  matrix  P  =  identity_matrix  -  coeff  *  w  *  w ^ T 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   P  *  x  =  [ normX ,  0 ,  0  ,  0 ,  . . . ] 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    *   coeff  -  scalar 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    *   w  =  [ 1 ,  w1 ,  w2 ,  w3 ,  . . . ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   w  =  u  /  u0 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   u  =  x  -  | x | * e0 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    *   u0  =  x0  -  | x | 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    *   e0  =  [ 1 ,  0 ,  0  ,  0 ,  . . . ] 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    * 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    *   x  -  input  vector ,  remains  unaffected 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   tail  -  the  essential  part  of  the  vector  w :  [ w1 ,  w2 ,  w3 ,  . . . ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   normX  -  this  scalar  is  the  first  non - zero  element  in  vector  resulting  from  Householder  transformation  - >  ( P * x ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    *   coeff  -  scalar ,  scaling  factor  in  Householder  matrix  formula 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    static  void  evalHHmatrixData ( const  NDArray &  x ,  NDArray &  tail ,  T &  coeff ,  T &  normX ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    static  void  evalHHmatrixDataI ( NDArray &  x ,  T &  coeff ,  T &  normX ) ;   // in-place, x to be affected
 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /**
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   this  method  mathematically  multiplies  input  matrix  on  Householder  from  the  left  P  *  matrix 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    * 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    *   matrix  -  input  matrix 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   tail  -  the  essential  part  of  the  Householder  vector  w :  [ w1 ,  w2 ,  w3 ,  . . . ] 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    *   coeff  -  scalar ,  scaling  factor  in  Householder  matrix  formula 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    static  void  mulLeft ( NDArray &  matrix ,  const  NDArray &  tail ,  const  T  coeff ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /**
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   this  method  mathematically  multiplies  input  matrix  on  Householder  from  the  right  matrix  *  P 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    * 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    *   matrix  -  input  matrix 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *   tail  -  the  essential  part  of  the  Householder  vector  w :  [ w1 ,  w2 ,  w3 ,  . . . ] 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    *   coeff  -  scalar ,  scaling  factor  in  Householder  matrix  formula 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    */ 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    static  void  mulRight ( NDArray &  matrix ,  const  NDArray &  tail ,  const  T  coeff ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // /**
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *  this function reduce given matrix to  upper bidiagonal form (in-place operation), matrix must satisfy following condition rows >= cols
 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // *
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *  matrix - input 2D matrix to be reduced to upper bidiagonal from
 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // */
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // template <typename T>
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // void biDiagonalizeUp(NDArray& matrix);
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // /**
 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // *  given a matrix [m,n], this function computes its singular value decomposition matrix = u * s * v^T
 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // *
 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // *  matrix - input 2D matrix to decompose, [m, n]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *  u - unitary matrix containing left singular vectors of input matrix, [m, m]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *  s - diagonal matrix with singular values of input matrix (non-negative) on the diagonal sorted in decreasing order,
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *      actually the mathematically correct dimension of s is [m, n], however for memory saving we work with s as vector [1, p], where p is smaller among m and n
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *  v - unitary matrix containing right singular vectors of input matrix, [n, n]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *  calcUV - if true then u and v will be computed, in opposite case function works significantly faster
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *  fullUV - if false then only p (p is smaller among m and n) first columns of u and v will be calculated and their dimensions in this case are [m, p] and [n, p]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // *
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // */
 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-14 18:06:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // void svd(const NDArray& matrix, NDArray& u, NDArray& s, NDArray& v, const bool calcUV = false, const bool fullUV = false)
 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-06 15:21:15 +03:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# endif  //LIBND4J_HOUSEHOLDER_H