diff --git a/.gitignore b/.gitignore index d66cc91..384e8e1 100644 --- a/.gitignore +++ b/.gitignore @@ -52,4 +52,5 @@ libla.la t test *.tar.bz2 +.*.swp # CVS default ignores end diff --git a/Makefile.am b/Makefile.am index d8d30c0..13b42b7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ lib_LTLIBRARIES = libla.la include_HEADERS = vecmat3.h quaternion.h fortran.h cuda_la.h auxstorage.h davidson.h laerror.h mat.h qsort.h vec.h bisection.h diis.h la.h noncblas.h smat.h bitvector.h fourindex.h la_traits.h nonclass.h sparsemat.h sparsesmat.h csrmat.h conjgrad.h gmres.h matexp.h permutation.h -libla_la_SOURCES = quaternion.cc vecmat3.cc vec.cc mat.cc smat.cc sparsemat.cc sparsesmat.cc csrmat.cc laerror.cc noncblas.cc bitvector.cc strassen.cc nonclass.cc cuda_la.cc fourindex.cc +libla_la_SOURCES = quaternion.cc vecmat3.cc vec.cc mat.cc smat.cc sparsemat.cc sparsesmat.cc csrmat.cc laerror.cc noncblas.cc bitvector.cc strassen.cc nonclass.cc cuda_la.cc fourindex.cc permutation.cc check_PROGRAMS = t test t_SOURCES = t.cc t2.cc test_SOURCES = test.cc diff --git a/configure.ac b/configure.ac index fa3fb08..06fb6ff 100644 --- a/configure.ac +++ b/configure.ac @@ -1,8 +1,8 @@ # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. -AC_PREREQ([2.63]) -AC_INIT([libla], [1.1], [jiri@pittnerovi.com]) +AC_PREREQ([2.71]) +AC_INIT([libla],[1.1],[jiri@pittnerovi.com]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_MACRO_DIR([m4]) AM_PROG_AR @@ -12,7 +12,7 @@ LT_INIT # Checks for programs. AC_PROG_CXX -AC_PROG_LIBTOOL +LT_INIT AC_PROG_CC #AC_PROG_RANLIB obsoleted by libtool AC_LANG(C++) diff --git a/permutation.cc b/permutation.cc new file mode 100644 index 0000000..6106002 --- /dev/null +++ b/permutation.cc @@ -0,0 +1,68 @@ +/* + LA: linear algebra C++ interface library + Copyright (C) 2021 Jiri Pittner or + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "permutation.h" +namespace LA { + +template +bool NRPerm::is_valid() const +{ +T n = this->size(); +if(n<0) return 0; +NRVec_from1 used(n); +used.clear(); +for(T i=1; i<=n; ++i) + { + T x= (*this)[i]; + if(x<1||x>n) return 0; + used[x] += 1; + } +for(T i=1; i<=n; ++i) if(used[i]!=1) return 0; +return 1; +} + +template +NRPerm NRPerm::inverse() const +{ +NRPerm q(this->size()); +for(T i=1; i<=this->size(); ++i) q[(*this)[i]]=i; +return q; +} + + +template +NRPerm NRPerm::operator*(const NRPerm q) const +{ +T n=this->size(); +if(n!=q.size()) laerror("product of incompatible permutations"); +NRPerm r(n); +for(T i=1; i<=n; ++i) r[i] = q[(*this)[i]]; +return r; + +} + + + + + + +/***************************************************************************//** + * forced instantization in the corresponding object file + ******************************************************************************/ +template class NRPerm; +} diff --git a/permutation.h b/permutation.h index 189ad94..b6e714f 100644 --- a/permutation.h +++ b/permutation.h @@ -1,6 +1,6 @@ /* LA: linear algebra C++ interface library - Copyright (C) 2008 Jiri Pittner or + Copyright (C) 2021 Jiri Pittner or This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,17 +15,46 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ + + #ifndef _PERMUTATION_H #define _PERMUTATION_H + +#include "la_traits.h" +#include "vec.h" +#include "mat.h" + +//permutations are always numbered from 1; offset is employed when applied to vectors and matrices + namespace LA { -template -const NRVec inversepermutation(const NRVec &p, const T offset=0) -{ -int n=p.size(); -NRVec q(n); -if(!offset) for(int i=0; i +class NRPerm : public NRVec_from1 { +public: + //basic constructors + NRPerm(): NRVec_from1() {}; + NRPerm(const int n) : NRVec_from1(n) {}; + NRPerm(const NRVec_from1 &rhs): NRVec_from1(rhs) {}; + NRPerm(const T &a, const int n): NRVec_from1(a, n) {}; + NRPerm(const T *a, const int n): NRVec_from1(a, n) {}; + + //specific operations + bool is_valid() const; //is it really a permutation + NRPerm inverse() const; + NRPerm operator*(const NRPerm rhs) const; + + //TODO: + //@@@conjugate by q + //@@@permgener + //@@@lex rank + //@@@next permutation + //@@@inversion tables + //@@@parity + //@@@conversion to cycle structure and back +}; + + + + }//namespace #endif