github.com/rmera/gochem@v0.7.1/coord_test.go (about)

     1  // +build matrix
     2  
     3  /*
     4   * coord_test.go
     5   *
     6   * Copyright 2013 Raul Mera <rmera@zinc>
     7   *
     8   * This program is free software; you can redistribute it and/or modify
     9   * it under the terms of the GNU General Public License as published by
    10   * the Free Software Foundation; either version 2 of the License, or
    11   * (at your option) any later version.
    12   *
    13   * This program is distributed in the hope that it will be useful,
    14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16   * GNU General Public License for more details.
    17   *
    18   * You should have received a copy of the GNU General Public License
    19   * along with this program; if not, write to the Free Software
    20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    21   * MA 02110-1301, USA.
    22   *
    23   *
    24   */
    25  
    26  package chem
    27  
    28  import (
    29  	"fmt"
    30  	"testing"
    31  
    32  	v3 "github.com/rmera/gochem/v3"
    33  )
    34  
    35  func TestGeo(Te *testing.T) {
    36  	a := []float64{1.0, 2.0, 3, 4, 5, 6, 7, 8, 9}
    37  	A, _ := v3.NewMatrix(a)
    38  	ar, ac := A.Dims()
    39  	T := v3.Zeros(ar)
    40  	T.T(A)
    41  	B := gnEye(ar)
    42  	//B.Copy(A)
    43  	T.Mul(A, B)
    44  	E := v3.Zeros(ar)
    45  	E.MulElem(A, B)
    46  	fmt.Println(T, "\n", T, "\n", A, "\n", B, "\n", ar, ac, A.Sum())
    47  	View := v3.Zeros(1)
    48  	View.VecView(A, 0)
    49  	View.Set(0, 0, 100)
    50  	fmt.Println("View\n", A, "\n", View)
    51  
    52  }
    53  
    54  func TestSomeVecs(Te *testing.T) {
    55  	a := []float64{1.0, 2.0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
    56  	A := v3.NewMatrix(a)
    57  	B := v3.Zeros(3) //We should cause an error by modifying this.
    58  	cind := []int{1, 3, 5}
    59  	err := B.SomeVecsSafe(A, cind)
    60  	if err != nil {
    61  		Te.Error(err)
    62  	}
    63  	fmt.Println(A, "\n", B)
    64  	B.Set(1, 1, 55)
    65  	B.Set(2, 2, 66)
    66  	fmt.Println("Changed B")
    67  	fmt.Println(A, "\n", B)
    68  	A.SetVecs(B, cind)
    69  	fmt.Println("Now A should see changes in B")
    70  	fmt.Println(A, "\n", B)
    71  }
    72  
    73  func TestScale(Te *testing.T) {
    74  	a := []float64{1.0, 2.0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
    75  	A := v3.NewMatrix(a)
    76  	B := v3.Zeros(6, 3)
    77  	A.Scale(3, A)
    78  	B.Scale(2, A)
    79  	fmt.Println(A, "\n", B)
    80  	Row := v3.NewMatrix([]float64{10, 20, 30})
    81  	A.AddVec(A, Row)
    82  	fmt.Println("Additions")
    83  	fmt.Println(A)
    84  	A.SubVec(A, Row)
    85  	fmt.Println(A)
    86  	B.Pow(A, 2)
    87  	fmt.Println("Squared", A, "\n", B)
    88  	b := []float64{1.0, 2.0, 3, 4, 5, 6, 7, 8, 9}
    89  	S := v3.NewMatrix(b)
    90  	row := v3.NewMatrix([]float64{2, 2, 3})
    91  	fmt.Println("Before scale", S, "\n", row)
    92  	S.ScaleByVec(S, row)
    93  	fmt.Println("Scaled by row", S)
    94  	col := v3.Zeros(3, 1)
    95  	col.T(row)
    96  	fmt.Println("Transpose", col)
    97  	S.ScaleByCol(S, col)
    98  	fmt.Println("Scaled by col", S)
    99  	rows2 := v3.NewMatrix([]float64{2, 2, 2, 3, 3, 3})
   100  	fmt.Println("Before adding 4", rows2)
   101  	rows2.AddFloat(rows2, 4)
   102  	fmt.Println("After adding 4", rows2)
   103  }
   104  
   105  func TestRowMod(Te *testing.T) {
   106  	a := []float64{1.0, 2.0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
   107  	A := v3.NewMatrix(a)
   108  	B := v3.Zeros(5, 3)
   109  	B.DelVec(A, 3)
   110  	fmt.Println("with and wihout row 3\n", A, "\n", B)
   111  	fmt.Println("test for Unit")
   112  	row := v3.NewMatrix([]float64{2, 2, 3})
   113  	fmt.Println("Original vector", row)
   114  	row.Unit(row)
   115  	fmt.Println("Unitarized", row)
   116  
   117  }
   118  
   119  func TestEigen(Te *testing.T) {
   120  	a := []float64{1, 2, 0, 2, 1, 0, 0, 0, 1}
   121  	A := v3.NewMatrix(a)
   122  	evecs, evals, err := gnEigen(A, -1)
   123  	fmt.Println(evecs, "\n", evals, "\n", err)
   124  	U, s, V, err := gnSVD(A)
   125  	fmt.Println("U\n", U, "\nsigma\n", s, "V\n", V, "\n", err)
   126  
   127  }