implementation of permutations in progress

This commit is contained in:
Jiri Pittner 2021-05-13 15:01:51 +02:00
parent 36d473dfb9
commit 01665674c5
5 changed files with 112 additions and 14 deletions

1
.gitignore vendored
View File

@ -52,4 +52,5 @@ libla.la
t
test
*.tar.bz2
.*.swp
# CVS default ignores end

View File

@ -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

View File

@ -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++)

68
permutation.cc Normal file
View File

@ -0,0 +1,68 @@
/*
LA: linear algebra C++ interface library
Copyright (C) 2021 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
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 <http://www.gnu.org/licenses/>.
*/
#include "permutation.h"
namespace LA {
template <typename T>
bool NRPerm<T>::is_valid() const
{
T n = this->size();
if(n<0) return 0;
NRVec_from1<T> 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 <typename T>
NRPerm<T> NRPerm<T>::inverse() const
{
NRPerm<T> q(this->size());
for(T i=1; i<=this->size(); ++i) q[(*this)[i]]=i;
return q;
}
template <typename T>
NRPerm<T> NRPerm<T>::operator*(const NRPerm<T> q) const
{
T n=this->size();
if(n!=q.size()) laerror("product of incompatible permutations");
NRPerm<T> 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<int>;
}

View File

@ -1,6 +1,6 @@
/*
LA: linear algebra C++ interface library
Copyright (C) 2008 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
Copyright (C) 2021 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
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 <http://www.gnu.org/licenses/>.
*/
#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<typename T>
const NRVec<T> inversepermutation(const NRVec<T> &p, const T offset=0)
{
int n=p.size();
NRVec<T> q(n);
if(!offset) for(int i=0; i<n; ++i) q[p[i]]=i;
else for(int i=0; i<n; ++i) q[p[i]-offset]=i+offset;
return q;
}
template <typename T>
class NRPerm : public NRVec_from1<T> {
public:
//basic constructors
NRPerm(): NRVec_from1<T>() {};
NRPerm(const int n) : NRVec_from1<T>(n) {};
NRPerm(const NRVec_from1<T> &rhs): NRVec_from1<T>(rhs) {};
NRPerm(const T &a, const int n): NRVec_from1<T>(a, n) {};
NRPerm(const T *a, const int n): NRVec_from1<T>(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