Kronecker product: Difference between revisions

m
C++ - changed output formatting
m (C++ - faster Kronecker product calculation)
m (C++ - changed output formatting)
Line 924:
 
template <typename scalar_type>
void print(std::ostreamwostream& out, const matrix<scalar_type>& a) {
const wchar_t* box_top_left = L"\x250c";
const wchar_t* box_top_right = L"\x2510";
const wchar_t* box_bottom_left = L"\x2514";
const wchar_t* box_bottom_right = L"\x2518";
const wchar_t* box_vertical = L"\x2502";
const wchar_t nl = L'\n';
const wchar_t space = L' ';
const int width = 2;
 
size_t rows = a.rows(), columns = a.columns();
std::wstring spaces((width + 1) * columns, space);
out << box_top_left << spaces << box_top_right << nl;
for (size_t row = 0; row < rows; ++row) {
out << box_vertical;
for (size_t column = 0; column < columns; ++column) {
if (column > 0)
out << ' 'space;
out << std::setw(3width) << a(row, column);
}
out << '\n'space << box_vertical << nl;
}
out << box_bottom_left << spaces << box_bottom_right << nl;
}
 
Line 940 ⟶ 953:
matrix<int> matrix2(2, 2, {{0,5}, {6,7}});
matrix<int> kp = kronecker_product(matrix1, matrix2);
std::coutwcout << L"Test case 1:\n";
print(std::coutwcout, kp);
}
 
Line 948 ⟶ 961:
matrix<int> matrix2(3, 4, {{1,1,1,1}, {1,0,0,1}, {1,1,1,1}});
matrix<int> kp = kronecker_product(matrix1, matrix2);
std::coutwcout << L"Test case 2:\n";
print(std::coutwcout, kp);
}
 
int main() {
std::wcout.imbue(std::locale(""));
test1();
test2();
Line 961 ⟶ 975:
<pre>
Test case 1:
0 5 0 10
0 6 5 70 10 12 14
6 0 7 1512 14 0 20
18 0 2115 240 20 28
│18 21 24 28 │
└ ┘
Test case 2:
0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 0 0 1 1 0 0 0 0
0 0 0 0 1 0 1 0 1 1 0 0 0 0 0
0 1 0 10 1 0 1 1 1 1 0 1 0 10 0 1 1 1
1 1 0 1 0 1 1 1 1 0 1 0 1 1 1 1 0 0 1
1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1
1 0 1 01 1 0 1 0 1 1 1 1 1 0 0 01 1 0
0 0 0 0 1 1 0 0 1 1 0 0 0 0
0 0 0 0 1 0 1 0 1 1 0 0 0 0 0
│ 0 0 0 0 1 1 1 1 0 0 0 0 │
└ ┘
</pre>
 
1,777

edits