Macaulay2 Engine
Loading...
Searching...
No Matches
mat-util.hpp
Go to the documentation of this file.
1// Copyright 2013 Michael E. Stillman
2
3#ifndef _mat_util_hpp_
4#define _mat_util_hpp_
5
34
35// Functions for all mutable matrices, or that don't fit in
36// mat-elem-ops, mat-arith, or mat-linalg
37
38#include <assert.h>
39#include "buffer.hpp"
40#include "text-io.hpp"
41
42template <typename Mat>
43void displayMat(buffer& o, const Mat& A)
44{
45 // Assumption: Mat is either DMat<RingType> or SMat<RingType>, in that it
46 // defines the following:
47 // Mat::ElementType
48 // A.ring()
49 // A.numRows(), A.numColumns()
50 // A.entry(r,c)
51 // and A.ring().elem_text_out
52
53 size_t nrows = A.numRows();
54 size_t ncols = A.numColumns();
55 buffer* p = new buffer[nrows];
56 size_t r;
57 for (size_t c = 0; c < ncols; c++)
58 {
59 size_t maxcount = 0;
60 for (r = 0; r < nrows; r++)
61 {
62 const typename Mat::ElementType& a = A.entry(r, c);
63 if (!A.ring().is_zero(a))
64 A.ring().elem_text_out(p[r], a, true, false, false);
65 else
66 p[r] << ".";
67 if (p[r].size() > maxcount) maxcount = p[r].size();
68 }
69 for (r = 0; r < nrows; r++)
70 for (size_t k = maxcount + 1 - p[r].size(); k > 0; k--) p[r] << ' ';
71 }
72 for (r = 0; r < nrows; r++)
73 {
74 p[r] << '\0';
75 char* s = p[r].str();
76 o << s << newline;
77 }
78 delete[] p;
79}
80
81template <typename Mat>
82void displayMat(const Mat& A)
83{
84 buffer o;
85 displayMat(o, A);
86 emit(o.str());
87}
88
89template <typename Mat>
90static void concatenateMatrices(const Mat& A, const Mat& B, Mat& C)
91{
92 assert(A.numRows() == B.numRows());
93 C.resize(A.numRows(), A.numColumns() + B.numColumns());
94 for (long r = 0; r < A.numRows(); r++)
95 for (long c = 0; c < A.numColumns(); c++)
96 A.ring().set(C.entry(r, c), A.entry(r, c));
97 for (long r = 0; r < A.numRows(); r++)
98 for (long c = 0; c < B.numColumns(); c++)
99 A.ring().set(C.entry(r, c + A.numColumns()), B.entry(r, c));
100}
101
102#endif
103
104// Local Variables:
105// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
106// indent-tabs-mode: nil
107// End:
Append-only GC-backed byte buffer used throughout the engine for text output.
char * str()
Definition buffer.hpp:72
int p
void size_t s
Definition m2-mem.cpp:271
char newline[]
Definition m2-types.cpp:49
void displayMat(buffer &o, const Mat &A)
Definition mat-util.hpp:43
static void concatenateMatrices(const Mat &A, const Mat &B, Mat &C)
Definition mat-util.hpp:90
void emit(const char *s)
Definition text-io.cpp:41
Text-formatting helpers layered on buffer: bignum print, line wrapping, M2_gbTrace-gated emit.