Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563550 views
1
// Collects equal columns of a matrix and lists each with multiplicity
2
3
#include <stdlib.h>
4
#include <vector>
5
#include <list>
6
#include <map>
7
8
#include <sstream>
9
#include <fstream>
10
#include <iostream>
11
#include <algorithm>
12
using namespace std;
13
14
15
vector<vector<int> > readMat(const string& project){
16
// reads one matrix from .in file
17
18
string name_in=project;
19
const char* file_in=name_in.c_str();
20
ifstream in;
21
in.open(file_in,ifstream::in);
22
if (in.is_open()==false){
23
cerr << "Cannot find input file" << endl;
24
exit(1);
25
}
26
27
int nrows,ncols;
28
in >> nrows;
29
in >> ncols;
30
31
if(nrows==0 || ncols==0){
32
cerr << "Matrix empty" << endl;
33
exit(1);
34
}
35
36
37
int i,j,entry;
38
vector<vector<int> > result(nrows);
39
40
for(i=0;i<nrows;++i)
41
for(j=0;j<ncols;++j){
42
in >> entry;
43
result[i].push_back(entry);
44
}
45
return(result);
46
}
47
48
int main(int argc, char* argv[])
49
{
50
51
if(argc<2){
52
cerr << "No input file given" << endl;
53
exit(1);
54
}
55
string input_name=argv[1];
56
vector< vector<int > > M, MT;
57
M=readMat(input_name);
58
MT.resize(M[1].size());
59
for(size_t i=0;i<M.size();++i)
60
for(size_t j=0;j<MT.size();++j)
61
MT[j].push_back(M[i][j]);
62
63
map< vector<int>, size_t > classes;
64
map< vector<int>, size_t >::iterator C;
65
66
for(size_t j=0;j<MT.size();++j){
67
C=classes.find(MT[j]);
68
if(C!=classes.end())
69
C->second++;
70
else
71
classes.insert(pair<vector<int>, size_t>(MT[j],1));
72
}
73
74
for(size_t i=0;i<M.size();++i){
75
for(C=classes.begin();C!=classes.end();++C)
76
cout << C->first[i] << " ";
77
cout << endl;
78
}
79
cout << "--------------" << endl;
80
81
for(C=classes.begin();C!=classes.end();++C)
82
cout << C->second << " ";
83
84
cout << endl << endl;
85
86
return(0);
87
}
88
89