A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
1
Task/Matrix-transposition/0DESCRIPTION
Normal file
1
Task/Matrix-transposition/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
[[wp:Transpose|Transpose]] an arbitrarily sized rectangular [[wp:Matrix (mathematics)|Matrix]].
|
||||
2
Task/Matrix-transposition/1META.yaml
Normal file
2
Task/Matrix-transposition/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Matrices
|
||||
17
Task/Matrix-transposition/ACL2/matrix-transposition.acl2
Normal file
17
Task/Matrix-transposition/ACL2/matrix-transposition.acl2
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defun cons-each (xs xss)
|
||||
(if (or (endp xs) (endp xss))
|
||||
nil
|
||||
(cons (cons (first xs) (first xss))
|
||||
(cons-each (rest xs) (rest xss)))))
|
||||
|
||||
(defun list-each (xs)
|
||||
(if (endp xs)
|
||||
nil
|
||||
(cons (list (first xs))
|
||||
(list-each (rest xs)))))
|
||||
|
||||
(defun transpose-list (xss)
|
||||
(if (endp (rest xss))
|
||||
(list-each (first xss))
|
||||
(cons-each (first xss)
|
||||
(transpose-list (rest xss)))))
|
||||
27
Task/Matrix-transposition/ALGOL-68/matrix-transposition.alg
Normal file
27
Task/Matrix-transposition/ALGOL-68/matrix-transposition.alg
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
main:(
|
||||
|
||||
[,]REAL m=((1, 1, 1, 1),
|
||||
(2, 4, 8, 16),
|
||||
(3, 9, 27, 81),
|
||||
(4, 16, 64, 256),
|
||||
(5, 25,125, 625));
|
||||
|
||||
OP ZIP = ([,]REAL in)[,]REAL:(
|
||||
[2 LWB in:2 UPB in,1 LWB in:1UPB in]REAL out;
|
||||
FOR i FROM LWB in TO UPB in DO
|
||||
out[,i]:=in[i,]
|
||||
OD;
|
||||
out
|
||||
);
|
||||
|
||||
PROC pprint = ([,]REAL m)VOID:(
|
||||
FORMAT real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals #
|
||||
FORMAT vec fmt = $"("n(2 UPB m-1)(f(real fmt)",")f(real fmt)")"$;
|
||||
FORMAT matrix fmt = $x"("n(UPB m-1)(f(vec fmt)","lxx)f(vec fmt)");"$;
|
||||
# finally print the result #
|
||||
printf((matrix fmt,m))
|
||||
);
|
||||
|
||||
printf(($x"Transpose:"l$));
|
||||
pprint((ZIP m))
|
||||
)
|
||||
8
Task/Matrix-transposition/APL/matrix-transposition.apl
Normal file
8
Task/Matrix-transposition/APL/matrix-transposition.apl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
3 3⍴⍳10
|
||||
1 2 3
|
||||
4 5 6
|
||||
7 8 9
|
||||
⍉ 3 3⍴⍳10
|
||||
1 4 7
|
||||
2 5 8
|
||||
3 6 9
|
||||
14
Task/Matrix-transposition/AWK/matrix-transposition.awk
Normal file
14
Task/Matrix-transposition/AWK/matrix-transposition.awk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# syntax: GAWK -f MATRIX_TRANSPOSITION.AWK filename
|
||||
{ if (NF > nf) {
|
||||
nf = NF
|
||||
}
|
||||
for (i=1; i<=nf; i++) {
|
||||
row[i] = row[i] $i " "
|
||||
}
|
||||
}
|
||||
END {
|
||||
for (i=1; i<=nf; i++) {
|
||||
printf("%s\n",row[i])
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
function transpose( m:Array):Array
|
||||
{
|
||||
//Assume each element in m is an array. (If this were production code, use typeof to be sure)
|
||||
|
||||
//Each element in m is a row, so this gets the length of a row in m,
|
||||
//which is the same as the number of rows in m transpose.
|
||||
var mTranspose = new Array(m[0].length);
|
||||
for(var i:uint = 0; i < mTranspose.length; i++)
|
||||
{
|
||||
//create a row
|
||||
mTranspose[i] = new Array(m.length);
|
||||
//set the row to the appropriate values
|
||||
for(var j:uint = 0; j < mTranspose[i].length; j++)
|
||||
mTranspose[i][j] = m[j][i];
|
||||
}
|
||||
return mTranspose;
|
||||
}
|
||||
var m:Array = [[1, 2, 3, 10],
|
||||
[4, 5, 6, 11],
|
||||
[7, 8, 9, 12]];
|
||||
var M:Array = transpose(m);
|
||||
for(var i:uint = 0; i < M.length; i++)
|
||||
trace(M[i]);
|
||||
27
Task/Matrix-transposition/Ada/matrix-transposition.ada
Normal file
27
Task/Matrix-transposition/Ada/matrix-transposition.ada
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Matrix_Transpose is
|
||||
procedure Put (X : Real_Matrix) is
|
||||
type Fixed is delta 0.01 range -500.0..500.0;
|
||||
begin
|
||||
for I in X'Range (1) loop
|
||||
for J in X'Range (2) loop
|
||||
Put (Fixed'Image (Fixed (X (I, J))));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Put;
|
||||
|
||||
Matrix : constant Real_Matrix :=
|
||||
( (0.0, 0.1, 0.2, 0.3),
|
||||
(0.4, 0.5, 0.6, 0.7),
|
||||
(0.8, 0.9, 1.0, 1.1)
|
||||
);
|
||||
begin
|
||||
Put_Line ("Before Transposition:");
|
||||
Put (Matrix);
|
||||
New_Line;
|
||||
Put_Line ("After Transposition:");
|
||||
Put (Transpose (Matrix));
|
||||
end Matrix_Transpose;
|
||||
14
Task/Matrix-transposition/Agda/matrix-transposition-1.agda
Normal file
14
Task/Matrix-transposition/Agda/matrix-transposition-1.agda
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
module Matrix where
|
||||
|
||||
open import Data.Nat
|
||||
open import Data.Vec
|
||||
|
||||
Matrix : (A : Set) → ℕ → ℕ → Set
|
||||
Matrix A m n = Vec (Vec A m) n
|
||||
|
||||
transpose : ∀ {A m n} → Matrix A m n → Matrix A n m
|
||||
transpose [] = replicate []
|
||||
transpose (xs ∷ xss) = zipWith _∷_ xs (transpose xss)
|
||||
|
||||
a = (1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ []
|
||||
b = transpose a
|
||||
|
|
@ -0,0 +1 @@
|
|||
(1 ∷ 4 ∷ []) ∷ (2 ∷ 5 ∷ []) ∷ (3 ∷ 6 ∷ []) ∷ []
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
a = a
|
||||
m = 10
|
||||
n = 10
|
||||
Loop, 10
|
||||
{
|
||||
i := A_Index - 1
|
||||
Loop, 10
|
||||
{
|
||||
j := A_Index - 1
|
||||
%a%%i%%j% := i - j
|
||||
}
|
||||
}
|
||||
before := matrix_print("a", m, n)
|
||||
transpose("a", m, n)
|
||||
after := matrix_print("a", m, n)
|
||||
MsgBox % before . "`ntransposed:`n" . after
|
||||
Return
|
||||
|
||||
transpose(a, m, n)
|
||||
{
|
||||
Local i, j, row, matrix
|
||||
Loop, % m
|
||||
{
|
||||
i := A_Index - 1
|
||||
Loop, % n
|
||||
{
|
||||
j := A_Index - 1
|
||||
temp%i%%j% := %a%%j%%i%
|
||||
}
|
||||
}
|
||||
Loop, % m
|
||||
{
|
||||
i := A_Index - 1
|
||||
Loop, % n
|
||||
{
|
||||
j := A_Index - 1
|
||||
%a%%i%%j% := temp%i%%j%
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
matrix_print(a, m, n)
|
||||
{
|
||||
Local i, j, row, matrix
|
||||
Loop, % m
|
||||
{
|
||||
i := A_Index - 1
|
||||
row := ""
|
||||
Loop, % n
|
||||
{
|
||||
j := A_Index - 1
|
||||
row .= %a%%i%%j% . ","
|
||||
}
|
||||
StringTrimRight, row, row, 1
|
||||
matrix .= row . "`n"
|
||||
}
|
||||
Return matrix
|
||||
}
|
||||
22
Task/Matrix-transposition/BBC-BASIC/matrix-transposition.bbc
Normal file
22
Task/Matrix-transposition/BBC-BASIC/matrix-transposition.bbc
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
INSTALL @lib$+"ARRAYLIB"
|
||||
|
||||
DIM matrix(3,4), transpose(4,3)
|
||||
matrix() = 78,19,30,12,36,49,10,65,42,50,30,93,24,78,10,39,68,27,64,29
|
||||
|
||||
PROC_transpose(matrix(), transpose())
|
||||
|
||||
FOR row% = 0 TO DIM(matrix(),1)
|
||||
FOR col% = 0 TO DIM(matrix(),2)
|
||||
PRINT ;matrix(row%,col%) " ";
|
||||
NEXT
|
||||
PRINT
|
||||
NEXT row%
|
||||
|
||||
PRINT
|
||||
|
||||
FOR row% = 0 TO DIM(transpose(),1)
|
||||
FOR col% = 0 TO DIM(transpose(),2)
|
||||
PRINT ;transpose(row%,col%) " ";
|
||||
NEXT
|
||||
PRINT
|
||||
NEXT row%
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
blsq ) {{78 19 30 12 36}{49 10 65 42 50}{30 93 24 78 10}{39 68 27 64 29}}tpsp
|
||||
78 49 30 39
|
||||
19 10 93 68
|
||||
30 65 24 27
|
||||
12 42 78 64
|
||||
36 50 10 29
|
||||
15
Task/Matrix-transposition/C++/matrix-transposition-1.cpp
Normal file
15
Task/Matrix-transposition/C++/matrix-transposition-1.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#include <boost/numeric/ublas/matrix.hpp>
|
||||
#include <boost/numeric/ublas/io.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost::numeric::ublas;
|
||||
|
||||
matrix<double> m(3,3);
|
||||
|
||||
for(int i=0; i!=m.size1(); ++i)
|
||||
for(int j=0; j!=m.size2(); ++j)
|
||||
m(i,j)=3*i+j;
|
||||
|
||||
std::cout << trans(m) << std::endl;
|
||||
}
|
||||
39
Task/Matrix-transposition/C++/matrix-transposition-2.cpp
Normal file
39
Task/Matrix-transposition/C++/matrix-transposition-2.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <iostream>
|
||||
#include "matrix.h"
|
||||
|
||||
#if !defined(ARRAY_SIZE)
|
||||
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
|
||||
#endif
|
||||
|
||||
template<class T>
|
||||
void printMatrix(const Matrix<T>& m) {
|
||||
std::cout << "rows = " << m.rowNum() << " columns = " << m.colNum() << std::endl;
|
||||
for (unsigned int i = 0; i < m.rowNum(); i++) {
|
||||
for (unsigned int j = 0; j < m.colNum(); j++) {
|
||||
std::cout << m[i][j] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
} /* printMatrix() */
|
||||
|
||||
int main() {
|
||||
int am[2][3] = {
|
||||
{1,2,3},
|
||||
{4,5,6},
|
||||
};
|
||||
|
||||
Matrix<int> a(ARRAY_SIZE(am), ARRAY_SIZE(am[0]), am[0], ARRAY_SIZE(am)*ARRAY_SIZE(am[0]));
|
||||
|
||||
try {
|
||||
std::cout << "Before transposition:" << std::endl;
|
||||
printMatrix(a);
|
||||
std::cout << std::endl;
|
||||
a.transpose();
|
||||
std::cout << "After transposition:" << std::endl;
|
||||
printMatrix(a);
|
||||
} catch (MatrixException& e) {
|
||||
std::cerr << e.message() << std::endl;
|
||||
return e.errorCode();
|
||||
}
|
||||
|
||||
} /* main() */
|
||||
195
Task/Matrix-transposition/C++/matrix-transposition-3.cpp
Normal file
195
Task/Matrix-transposition/C++/matrix-transposition-3.cpp
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
#ifndef _MATRIX_H
|
||||
#define _MATRIX_H
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#define MATRIX_ERROR_CODE_COUNT 5
|
||||
#define MATRIX_ERR_UNDEFINED "1 Undefined exception!"
|
||||
#define MATRIX_ERR_WRONG_ROW_INDEX "2 The row index is out of range."
|
||||
#define MATRIX_ERR_MUL_ROW_AND_COL_NOT_EQUAL "3 The row number of second matrix must be equal with the column number of first matrix!"
|
||||
#define MATRIX_ERR_MUL_ROW_AND_COL_BE_GREATER_THAN_ZERO "4 The number of rows and columns must be greater than zero!"
|
||||
#define MATRIX_ERR_TOO_FEW_DATA "5 Too few data in matrix."
|
||||
|
||||
class MatrixException {
|
||||
private:
|
||||
std::string message_;
|
||||
int errorCode_;
|
||||
public:
|
||||
MatrixException(std::string message = MATRIX_ERR_UNDEFINED);
|
||||
|
||||
inline std::string message() {
|
||||
return message_;
|
||||
};
|
||||
|
||||
inline int errorCode() {
|
||||
return errorCode_;
|
||||
};
|
||||
};
|
||||
|
||||
MatrixException::MatrixException(std::string message) {
|
||||
errorCode_ = MATRIX_ERROR_CODE_COUNT + 1;
|
||||
std::stringstream ss(message);
|
||||
ss >> errorCode_;
|
||||
if (errorCode_ < 1) {
|
||||
errorCode_ = MATRIX_ERROR_CODE_COUNT + 1;
|
||||
}
|
||||
std::string::size_type pos = message.find(' ');
|
||||
if (errorCode_ <= MATRIX_ERROR_CODE_COUNT && pos != std::string::npos) {
|
||||
message_ = message.substr(pos + 1);
|
||||
} else {
|
||||
message_ = message + " (This an unknown and unsupported exception!)";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic class for matrices.
|
||||
*/
|
||||
template <class T>
|
||||
class Matrix {
|
||||
private:
|
||||
std::vector<T> v; // the data of matrix
|
||||
unsigned int m; // the number of rows
|
||||
unsigned int n; // the number of columns
|
||||
protected:
|
||||
|
||||
virtual void clear() {
|
||||
v.clear();
|
||||
m = n = 0;
|
||||
}
|
||||
public:
|
||||
|
||||
Matrix() {
|
||||
clear();
|
||||
}
|
||||
Matrix(unsigned int, unsigned int, T* = 0, unsigned int = 0);
|
||||
Matrix(unsigned int, unsigned int, const std::vector<T>&);
|
||||
|
||||
virtual ~Matrix() {
|
||||
clear();
|
||||
}
|
||||
Matrix& operator=(const Matrix&);
|
||||
std::vector<T> operator[](unsigned int) const;
|
||||
Matrix operator*(const Matrix&);
|
||||
void transpose();
|
||||
|
||||
inline unsigned int rowNum() const {
|
||||
return m;
|
||||
}
|
||||
|
||||
inline unsigned int colNum() const {
|
||||
return n;
|
||||
}
|
||||
|
||||
inline unsigned int size() const {
|
||||
return v.size();
|
||||
}
|
||||
|
||||
inline void add(const T& t) {
|
||||
v.push_back(t);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
Matrix<T>::Matrix(unsigned int row, unsigned int col, T* data, unsigned int dataLength) {
|
||||
clear();
|
||||
if (row > 0 && col > 0) {
|
||||
m = row;
|
||||
n = col;
|
||||
unsigned int mxn = m * n;
|
||||
if (dataLength && data) {
|
||||
for (unsigned int i = 0; i < dataLength && i < mxn; i++) {
|
||||
v.push_back(data[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Matrix<T>::Matrix(unsigned int row, unsigned int col, const std::vector<T>& data) {
|
||||
clear();
|
||||
if (row > 0 && col > 0) {
|
||||
m = row;
|
||||
n = col;
|
||||
unsigned int mxn = m * n;
|
||||
if (data.size() > 0) {
|
||||
for (unsigned int i = 0; i < mxn && i < data.size(); i++) {
|
||||
v.push_back(data[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& other) {
|
||||
clear();
|
||||
if (other.m > 0 && other.n > 0) {
|
||||
m = other.m;
|
||||
n = other.n;
|
||||
unsigned int mxn = m * n;
|
||||
for (unsigned int i = 0; i < mxn && i < other.size(); i++) {
|
||||
v.push_back(other.v[i]);
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::vector<T> Matrix<T>::operator[](unsigned int index) const {
|
||||
std::vector<T> result;
|
||||
if (index >= m) {
|
||||
throw MatrixException(MATRIX_ERR_WRONG_ROW_INDEX);
|
||||
} else if ((index + 1) * n > size()) {
|
||||
throw MatrixException(MATRIX_ERR_TOO_FEW_DATA);
|
||||
} else {
|
||||
unsigned int begin = index * n;
|
||||
unsigned int end = begin + n;
|
||||
for (unsigned int i = begin; i < end; i++) {
|
||||
result.push_back(v[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Matrix<T> Matrix<T>::operator*(const Matrix<T>& other) {
|
||||
Matrix result(m, other.n);
|
||||
if (n != other.m) {
|
||||
throw MatrixException(MATRIX_ERR_MUL_ROW_AND_COL_NOT_EQUAL);
|
||||
} else if (m <= 0 || n <= 0 || other.n <= 0) {
|
||||
throw MatrixException(MATRIX_ERR_MUL_ROW_AND_COL_BE_GREATER_THAN_ZERO);
|
||||
} else if (m * n > size() || other.m * other.n > other.size()) {
|
||||
throw MatrixException(MATRIX_ERR_TOO_FEW_DATA);
|
||||
} else {
|
||||
for (unsigned int i = 0; i < m; i++) {
|
||||
for (unsigned int j = 0; j < other.n; j++) {
|
||||
T temp = v[i * n] * other.v[j];
|
||||
for (unsigned int k = 1; k < n; k++) {
|
||||
temp += v[i * n + k] * other.v[k * other.n + j];
|
||||
}
|
||||
result.v.push_back(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Matrix<T>::transpose() {
|
||||
if (m * n > size()) {
|
||||
throw MatrixException(MATRIX_ERR_TOO_FEW_DATA);
|
||||
} else {
|
||||
std::vector<T> v2;
|
||||
std::swap(v, v2);
|
||||
for (unsigned int i = 0; i < n; i++) {
|
||||
for (unsigned int j = 0; j < m; j++) {
|
||||
v.push_back(v2[j * n + i]);
|
||||
}
|
||||
}
|
||||
std::swap(m, n);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* _MATRIX_H */
|
||||
25
Task/Matrix-transposition/C/matrix-transposition-1.c
Normal file
25
Task/Matrix-transposition/C/matrix-transposition-1.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void transpose(void *dest, void *src, int src_h, int src_w)
|
||||
{
|
||||
int i, j;
|
||||
double (*d)[src_h] = dest, (*s)[src_w] = src;
|
||||
for (i = 0; i < src_h; i++)
|
||||
for (j = 0; j < src_w; j++)
|
||||
d[j][i] = s[i][j];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, j;
|
||||
double a[3][5] = {{ 0, 1, 2, 3, 4 },
|
||||
{ 5, 6, 7, 8, 9 },
|
||||
{ 1, 0, 0, 0, 42}};
|
||||
double b[5][3];
|
||||
transpose(b, a, 3, 5);
|
||||
|
||||
for (i = 0; i < 5; i++)
|
||||
for (j = 0; j < 3; j++)
|
||||
printf("%g%c", b[i][j], j == 2 ? '\n' : ' ');
|
||||
return 0;
|
||||
}
|
||||
50
Task/Matrix-transposition/C/matrix-transposition-2.c
Normal file
50
Task/Matrix-transposition/C/matrix-transposition-2.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void transpose(double *m, int w, int h)
|
||||
{
|
||||
int start, next, i;
|
||||
double tmp;
|
||||
|
||||
for (start = 0; start <= w * h - 1; start++) {
|
||||
next = start;
|
||||
i = 0;
|
||||
do { i++;
|
||||
next = (next % h) * w + next / h;
|
||||
} while (next > start);
|
||||
if (next < start || i == 1) continue;
|
||||
|
||||
tmp = m[next = start];
|
||||
do {
|
||||
i = (next % h) * w + next / h;
|
||||
m[next] = (i == start) ? tmp : m[i];
|
||||
next = i;
|
||||
} while (next > start);
|
||||
}
|
||||
}
|
||||
|
||||
void show_matrix(double *m, int w, int h)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < h; i++) {
|
||||
for (j = 0; j < w; j++)
|
||||
printf("%2g ", m[i * w + j]);
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
double m[15];
|
||||
for (i = 0; i < 15; i++) m[i] = i + 1;
|
||||
|
||||
puts("before transpose:");
|
||||
show_matrix(m, 3, 5);
|
||||
|
||||
transpose(m, 3, 5);
|
||||
|
||||
puts("\nafter transpose:");
|
||||
show_matrix(m, 5, 3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
11
Task/Matrix-transposition/Clojure/matrix-transposition-1.clj
Normal file
11
Task/Matrix-transposition/Clojure/matrix-transposition-1.clj
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(defmulti matrix-transpose
|
||||
"Switch rows with columns."
|
||||
class)
|
||||
|
||||
(defmethod matrix-transpose clojure.lang.PersistentList
|
||||
[mtx]
|
||||
(apply map list mtx))
|
||||
|
||||
(defmethod matrix-transpose clojure.lang.PersistentVector
|
||||
[mtx]
|
||||
(vec (apply map vector mtx)))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
=> (matrix-transpose [[1 2 3] [4 5 6]])
|
||||
[[1 4] [2 5] [3 6]]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
transpose = (matrix) ->
|
||||
(t[i] for t in matrix) for i in [0...matrix[0].length]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(defun transpose (m)
|
||||
(apply #'mapcar #'list m))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
;; Transpose a mxn matrix A to a nxm matrix B=A'.
|
||||
(defun mtp (A)
|
||||
(let* ((m (array-dimension A 0))
|
||||
(n (array-dimension A 1))
|
||||
(B (make-array `(,n ,m) :initial-element 0)))
|
||||
(loop for i from 0 below m do
|
||||
(loop for j from 0 below n do
|
||||
(setf (aref B j i)
|
||||
(aref A i j))))
|
||||
B))
|
||||
17
Task/Matrix-transposition/D/matrix-transposition-1.d
Normal file
17
Task/Matrix-transposition/D/matrix-transposition-1.d
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import std.stdio;
|
||||
|
||||
T[][] transpose(T)(immutable /*in*/ T[][] m) pure nothrow {
|
||||
auto r = new typeof(return)(m[0].length, m.length);
|
||||
foreach (nr, row; m)
|
||||
foreach (nc, c; row)
|
||||
r[nc][nr] = c;
|
||||
return r;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable M = [[10, 11, 12, 13],
|
||||
[14, 15, 16, 17],
|
||||
[18, 19, 20, 21]];
|
||||
immutable T = transpose(M);
|
||||
writefln("%(%(%2d %)\n%)", T);
|
||||
}
|
||||
13
Task/Matrix-transposition/D/matrix-transposition-2.d
Normal file
13
Task/Matrix-transposition/D/matrix-transposition-2.d
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
auto transpose(T)(in T[][] m) /*pure nothrow*/ {
|
||||
return iota(m[0].length).map!(i => transversal(m, i))();
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = [[10, 11, 12, 13],
|
||||
[14, 15, 16, 17],
|
||||
[18, 19, 20, 21]];
|
||||
/*immutable*/ auto T = transpose(M);
|
||||
writefln("%(%(%2d %)\n%)", T);
|
||||
}
|
||||
2
Task/Matrix-transposition/ELLA/matrix-transposition.ella
Normal file
2
Task/Matrix-transposition/ELLA/matrix-transposition.ella
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
MAC TRANSPOSE = ([INT n][INT m]TYPE t: matrix) -> [m][n]t:
|
||||
[INT i = 1..m] [INT j = 1..n] matrix[j][i].
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
function transpose(sequence in)
|
||||
sequence out
|
||||
out = repeat(repeat(0,length(in)),length(in[1]))
|
||||
for n = 1 to length(in) do
|
||||
for m = 1 to length(in[1]) do
|
||||
out[m][n] = in[n][m]
|
||||
end for
|
||||
end for
|
||||
return out
|
||||
end function
|
||||
|
||||
sequence m
|
||||
m = {
|
||||
{1,2,3,4},
|
||||
{5,6,7,8},
|
||||
{9,10,11,12}
|
||||
}
|
||||
|
||||
? transpose(m)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
( scratchpad ) { { 1 2 3 } { 4 5 6 } } flip .
|
||||
{ { 1 4 } { 2 5 } { 3 6 } }
|
||||
13
Task/Matrix-transposition/Fortran/matrix-transposition-1.f
Normal file
13
Task/Matrix-transposition/Fortran/matrix-transposition-1.f
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
integer, parameter :: n = 3, m = 5
|
||||
real, dimension(n,m) :: a = reshape( (/ (i,i=1,n*m) /), (/ n, m /) )
|
||||
real, dimension(m,n) :: b
|
||||
|
||||
b = transpose(a)
|
||||
|
||||
do i = 1, n
|
||||
print *, a(i,:)
|
||||
end do
|
||||
|
||||
do j = 1, m
|
||||
print *, b(j,:)
|
||||
end do
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
REAL A(3,5), B(5,3)
|
||||
DATA ((A(I,J),I=1,3),J=1,5) /1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15/
|
||||
|
||||
DO I = 1, 3
|
||||
DO J = 1, 5
|
||||
B(J,I) = A(I,J)
|
||||
END DO
|
||||
END DO
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
REAL A(3,5), B(5,3)
|
||||
DATA ((A(I,J),I=1,3),J=1,5) /1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15/
|
||||
|
||||
DO 10 I = 1, 3
|
||||
DO 20 J = 1, 5
|
||||
B(J,I) = A(I,J)
|
||||
20 CONTINUE
|
||||
10 CONTINUE
|
||||
6
Task/Matrix-transposition/GAP/matrix-transposition.gap
Normal file
6
Task/Matrix-transposition/GAP/matrix-transposition.gap
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
originalMatrix := [[1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25, 125, 625]];
|
||||
transposedMatrix := TransposedMat(originalMatrix);
|
||||
35
Task/Matrix-transposition/Go/matrix-transposition-1.go
Normal file
35
Task/Matrix-transposition/Go/matrix-transposition-1.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type row []float64
|
||||
type matrix []row
|
||||
|
||||
func main() {
|
||||
m := matrix{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
}
|
||||
printMatrix(m)
|
||||
t := transpose(m)
|
||||
printMatrix(t)
|
||||
}
|
||||
|
||||
func printMatrix(m matrix) {
|
||||
for _, s := range m {
|
||||
fmt.Println(s)
|
||||
}
|
||||
}
|
||||
|
||||
func transpose(m matrix) matrix {
|
||||
r := make(matrix, len(m[0]))
|
||||
for x, _ := range r {
|
||||
r[x] = make(row, len(m))
|
||||
}
|
||||
for y, s := range m {
|
||||
for x, e := range s {
|
||||
r[x][y] = e
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
51
Task/Matrix-transposition/Go/matrix-transposition-2.go
Normal file
51
Task/Matrix-transposition/Go/matrix-transposition-2.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type matrix struct {
|
||||
ele []float64
|
||||
stride int
|
||||
}
|
||||
|
||||
// construct new matrix from slice of slices
|
||||
func matrixFromRows(rows [][]float64) *matrix {
|
||||
if len(rows) == 0 {
|
||||
return &matrix{nil, 0}
|
||||
}
|
||||
m := &matrix{make([]float64, len(rows)*len(rows[0])), len(rows[0])}
|
||||
for rx, row := range rows {
|
||||
copy(m.ele[rx*m.stride:(rx+1)*m.stride], row)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := matrixFromRows([][]float64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
})
|
||||
m.print("original:")
|
||||
m.transpose().print("transpose:")
|
||||
}
|
||||
|
||||
func (m *matrix) print(heading string) {
|
||||
if heading > "" {
|
||||
fmt.Print("\n", heading, "\n")
|
||||
}
|
||||
for e := 0; e < len(m.ele); e += m.stride {
|
||||
fmt.Println(m.ele[e : e+m.stride])
|
||||
}
|
||||
}
|
||||
|
||||
func (m *matrix) transpose() *matrix {
|
||||
r := &matrix{make([]float64, len(m.ele)), len(m.ele) / m.stride}
|
||||
rx := 0
|
||||
for _, e := range m.ele {
|
||||
r.ele[rx] = e
|
||||
rx += r.stride
|
||||
if rx >= len(r.ele) {
|
||||
rx -= len(r.ele) - 1
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
73
Task/Matrix-transposition/Go/matrix-transposition-3.go
Normal file
73
Task/Matrix-transposition/Go/matrix-transposition-3.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type matrix struct {
|
||||
ele []float64
|
||||
stride int
|
||||
}
|
||||
|
||||
// construct new matrix from slice of slices
|
||||
func matrixFromRows(rows [][]float64) *matrix {
|
||||
if len(rows) == 0 {
|
||||
return &matrix{nil, 0}
|
||||
}
|
||||
m := &matrix{make([]float64, len(rows)*len(rows[0])), len(rows[0])}
|
||||
for rx, row := range rows {
|
||||
copy(m.ele[rx*m.stride:(rx+1)*m.stride], row)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := matrixFromRows([][]float64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
})
|
||||
m.print("original:")
|
||||
m.transposeInPlace()
|
||||
m.print("transpose:")
|
||||
}
|
||||
|
||||
func (m *matrix) print(heading string) {
|
||||
if heading > "" {
|
||||
fmt.Print("\n", heading, "\n")
|
||||
}
|
||||
for e := 0; e < len(m.ele); e += m.stride {
|
||||
fmt.Println(m.ele[e : e+m.stride])
|
||||
}
|
||||
}
|
||||
|
||||
func (m *matrix) transposeInPlace() {
|
||||
h := len(m.ele) / m.stride
|
||||
for start := range m.ele {
|
||||
next := start
|
||||
i := 0
|
||||
for {
|
||||
i++
|
||||
next = (next%h)*m.stride + next/h
|
||||
if next <= start {
|
||||
break
|
||||
}
|
||||
}
|
||||
if next < start || i == 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
next = start
|
||||
tmp := m.ele[next]
|
||||
for {
|
||||
i = (next%h)*m.stride + next/h
|
||||
if i == start {
|
||||
m.ele[next] = tmp
|
||||
} else {
|
||||
m.ele[next] = m.ele[i]
|
||||
}
|
||||
next = i
|
||||
if next <= start {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
m.stride = h
|
||||
}
|
||||
19
Task/Matrix-transposition/Go/matrix-transposition-4.go
Normal file
19
Task/Matrix-transposition/Go/matrix-transposition-4.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
mat "github.com/skelterjohn/go.matrix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := mat.MakeDenseMatrixStacked([][]float64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
})
|
||||
fmt.Println("original:")
|
||||
fmt.Println(m)
|
||||
m = m.Transpose()
|
||||
fmt.Println("transpose:")
|
||||
fmt.Println(m)
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
def matrix = [ [ 1, 2, 3, 4 ],
|
||||
[ 5, 6, 7, 8 ] ]
|
||||
|
||||
matrix.each { println it }
|
||||
println()
|
||||
def transpose = matrix.transpose()
|
||||
|
||||
transpose.each { println it }
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
*Main> transpose [[1,2],[3,4],[5,6]]
|
||||
[[1,3,5],[2,4,6]]
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import Data.Array
|
||||
|
||||
swap (x,y) = (y,x)
|
||||
|
||||
transpArray :: (Ix a, Ix b) => Array (a,b) e -> Array (b,a) e
|
||||
transpArray a = ixmap (swap l, swap u) swap a where
|
||||
(l,u) = bounds a
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
REAL :: mtx(2, 4)
|
||||
|
||||
mtx = 1.1 * $
|
||||
WRITE() mtx
|
||||
|
||||
SOLVE(Matrix=mtx, Transpose=mtx)
|
||||
WRITE() mtx
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
1.1 2.2 3.3 4.4
|
||||
5.5 6.6 7.7 8.8
|
||||
|
||||
1.1 5.5
|
||||
2.2 6.6
|
||||
3.3 7.7
|
||||
4.4 8.8
|
||||
4
Task/Matrix-transposition/Hope/matrix-transposition.hope
Normal file
4
Task/Matrix-transposition/Hope/matrix-transposition.hope
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
uses lists;
|
||||
dec transpose : list (list alpha) -> list (list alpha);
|
||||
--- transpose ([]::_) <= [];
|
||||
--- transpose n <= map head n :: transpose (map tail n);
|
||||
2
Task/Matrix-transposition/IDL/matrix-transposition.idl
Normal file
2
Task/Matrix-transposition/IDL/matrix-transposition.idl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
m=[[1,1,1,1],[2, 4, 8, 16],[3, 9,27, 81],[5, 25,125, 625]]
|
||||
print,transpose(m)
|
||||
28
Task/Matrix-transposition/Icon/matrix-transposition.icon
Normal file
28
Task/Matrix-transposition/Icon/matrix-transposition.icon
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
procedure transpose_matrix (matrix)
|
||||
result := []
|
||||
# for each column
|
||||
every (i := 1 to *matrix[1]) do {
|
||||
col := []
|
||||
# extract the number in each row for that column
|
||||
every (row := !matrix) do put (col, row[i])
|
||||
# and push that column as a row in the result matrix
|
||||
put (result, col)
|
||||
}
|
||||
return result
|
||||
end
|
||||
|
||||
procedure print_matrix (matrix)
|
||||
every (row := !matrix) do {
|
||||
every writes (!row || " ")
|
||||
write ()
|
||||
}
|
||||
end
|
||||
|
||||
procedure main ()
|
||||
matrix := [[1,2,3],[4,5,6]]
|
||||
write ("Start:")
|
||||
print_matrix (matrix)
|
||||
transposed := transpose_matrix (matrix)
|
||||
write ("Transposed:")
|
||||
print_matrix (transposed)
|
||||
end
|
||||
11
Task/Matrix-transposition/J/matrix-transposition.j
Normal file
11
Task/Matrix-transposition/J/matrix-transposition.j
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
]matrix=: (^/ }:) >:i.5 NB. make and show example matrix
|
||||
1 1 1 1
|
||||
2 4 8 16
|
||||
3 9 27 81
|
||||
4 16 64 256
|
||||
5 25 125 625
|
||||
|: matrix
|
||||
1 2 3 4 5
|
||||
1 4 9 16 25
|
||||
1 8 27 64 125
|
||||
1 16 81 256 625
|
||||
19
Task/Matrix-transposition/Java/matrix-transposition.java
Normal file
19
Task/Matrix-transposition/Java/matrix-transposition.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import java.util.Arrays;
|
||||
public class Transpose{
|
||||
public static void main(String[] args){
|
||||
double[][] m = {{1, 1, 1, 1},
|
||||
{2, 4, 8, 16},
|
||||
{3, 9, 27, 81},
|
||||
{4, 16, 64, 256},
|
||||
{5, 25, 125, 625}};
|
||||
double[][] ans = new double[m[0].length][m.length];
|
||||
for(int rows = 0; rows < m.length; rows++){
|
||||
for(int cols = 0; cols < m[0].length; cols++){
|
||||
ans[cols][rows] = m[rows][cols];
|
||||
}
|
||||
}
|
||||
for(double[] i:ans){//2D arrays are arrays of arrays
|
||||
System.out.println(Arrays.toString(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Task/Matrix-transposition/JavaScript/matrix-transposition.js
Normal file
29
Task/Matrix-transposition/JavaScript/matrix-transposition.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
function Matrix(ary) {
|
||||
this.mtx = ary
|
||||
this.height = ary.length;
|
||||
this.width = ary[0].length;
|
||||
}
|
||||
|
||||
Matrix.prototype.toString = function() {
|
||||
var s = []
|
||||
for (var i = 0; i < this.mtx.length; i++)
|
||||
s.push( this.mtx[i].join(",") );
|
||||
return s.join("\n");
|
||||
}
|
||||
|
||||
// returns a new matrix
|
||||
Matrix.prototype.transpose = function() {
|
||||
var transposed = [];
|
||||
for (var i = 0; i < this.width; i++) {
|
||||
transposed[i] = [];
|
||||
for (var j = 0; j < this.height; j++) {
|
||||
transposed[i][j] = this.mtx[j][i];
|
||||
}
|
||||
}
|
||||
return new Matrix(transposed);
|
||||
}
|
||||
|
||||
var m = new Matrix([[1,1,1,1],[2,4,8,16],[3,9,27,81],[4,16,64,256],[5,25,125,625]]);
|
||||
print(m);
|
||||
print();
|
||||
print(m.transpose());
|
||||
5
Task/Matrix-transposition/Joy/matrix-transposition.joy
Normal file
5
Task/Matrix-transposition/Joy/matrix-transposition.joy
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
DEFINE transpose == [ [null] [true] [[null] some] ifte ]
|
||||
[ pop [] ]
|
||||
[ [[first] map] [[rest] map] cleave ]
|
||||
[ cons ]
|
||||
linrec .
|
||||
12
Task/Matrix-transposition/K/matrix-transposition.k
Normal file
12
Task/Matrix-transposition/K/matrix-transposition.k
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
{x^\:-1_ x}1+!:5
|
||||
(1 1 1 1.0
|
||||
2 4 8 16.0
|
||||
3 9 27 81.0
|
||||
4 16 64 256.0
|
||||
5 25 125 625.0)
|
||||
|
||||
+{x^\:-1_ x}1+!:5
|
||||
(1 2 3 4 5.0
|
||||
1 4 9 16 25.0
|
||||
1 8 27 64 125.0
|
||||
1 16 81 256 625.0)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
12 iota [3 4] reshape 1 + dup .
|
||||
1 transpose .
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
MatrixC$ ="4, 3, 0, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 1.00, 1.10"
|
||||
|
||||
print "Transpose of matrix"
|
||||
call DisplayMatrix MatrixC$
|
||||
print " ="
|
||||
MatrixT$ =MatrixTranspose$( MatrixC$)
|
||||
call DisplayMatrix MatrixT$
|
||||
23
Task/Matrix-transposition/Lua/matrix-transposition.lua
Normal file
23
Task/Matrix-transposition/Lua/matrix-transposition.lua
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function Transpose( m )
|
||||
local res = {}
|
||||
|
||||
for i = 1, #m[1] do
|
||||
res[i] = {}
|
||||
for j = 1, #m do
|
||||
res[i][j] = m[j][i]
|
||||
end
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
-- a test for Transpose(m)
|
||||
mat = { { 1, 2, 3 }, { 4, 5, 6 } }
|
||||
erg = Transpose( mat )
|
||||
for i = 1, #erg do
|
||||
for j = 1, #erg[1] do
|
||||
io.write( erg[i][j] )
|
||||
io.write( " " )
|
||||
end
|
||||
io.write( "\n" )
|
||||
end
|
||||
13
Task/Matrix-transposition/MATLAB/matrix-transposition.m
Normal file
13
Task/Matrix-transposition/MATLAB/matrix-transposition.m
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
>> transpose([1 2;3 4])
|
||||
|
||||
ans =
|
||||
|
||||
1 3
|
||||
2 4
|
||||
|
||||
>> [1 2;3 4].'
|
||||
|
||||
ans =
|
||||
|
||||
1 3
|
||||
2 4
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
m = bigMatrix 5 4
|
||||
for i in 1 to 5 do for j in 1 to 4 do m[i][j] = pow i j
|
||||
m = transpose m
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
originalMatrix = {{1, 1, 1, 1},
|
||||
{2, 4, 8, 16},
|
||||
{3, 9, 27, 81},
|
||||
{4, 16, 64, 256},
|
||||
{5, 25, 125, 625}}
|
||||
transposedMatrix = Transpose[originalMatrix]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
originalMatrix : matrix([1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25, 125, 625]);
|
||||
transposedMatrix : transpose(originalMatrix);
|
||||
10
Task/Matrix-transposition/PHP/matrix-transposition.php
Normal file
10
Task/Matrix-transposition/PHP/matrix-transposition.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function transpose($m) {
|
||||
if (count($m) == 0) // special case: empty matrix
|
||||
return array();
|
||||
else if (count($m) == 1) // special case: row matrix
|
||||
return array_chunk($m[0], 1);
|
||||
|
||||
// array_map(NULL, m[0], m[1], ..)
|
||||
array_unshift($m, NULL); // the original matrix is not modified because it was passed by value
|
||||
return call_user_func_array('array_map', $m);
|
||||
}
|
||||
11
Task/Matrix-transposition/Perl/matrix-transposition-1.pl
Normal file
11
Task/Matrix-transposition/Perl/matrix-transposition-1.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use Math::Matrix;
|
||||
|
||||
$m = Math::Matrix->new(
|
||||
[1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25, 125, 625],
|
||||
);
|
||||
|
||||
$m->transpose->print;
|
||||
12
Task/Matrix-transposition/Perl/matrix-transposition-2.pl
Normal file
12
Task/Matrix-transposition/Perl/matrix-transposition-2.pl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
my @m = (
|
||||
[1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25, 125, 625],
|
||||
);
|
||||
|
||||
my @transposed;
|
||||
foreach my $j (0..$#{$m[0]}) {
|
||||
push(@transposed, [map $_->[$j], @m]);
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(de matTrans (Mat)
|
||||
(apply mapcar Mat list) )
|
||||
|
||||
(matTrans '((1 2 3) (4 5 6)))
|
||||
27
Task/Matrix-transposition/Prolog/matrix-transposition.pro
Normal file
27
Task/Matrix-transposition/Prolog/matrix-transposition.pro
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
% transposition of a rectangular matrix
|
||||
% e.g. [[1,2,3,4], [5,6,7,8]]
|
||||
% give [[1,5],[2,6],[3,7],[4,8]]
|
||||
|
||||
transpose(In, Out) :-
|
||||
In = [H | T],
|
||||
maplist(initdl, H, L),
|
||||
work(T, In, Out).
|
||||
|
||||
% we use the difference list to make "quick" appends (one inference)
|
||||
initdl(V, [V | X] - X).
|
||||
|
||||
work(Lst, [H], Out) :-
|
||||
maplist(my_append_last, Lst, H, Out).
|
||||
|
||||
work(Lst, [H | T], Out) :-
|
||||
maplist(my_append, Lst, H, Lst1),
|
||||
work(Lst1, T, Out).
|
||||
|
||||
my_append(X-Y, C, X1-Y1) :-
|
||||
append_dl(X-Y, [C | U]- U, X1-Y1).
|
||||
|
||||
my_append_last(X-Y, C, X1) :-
|
||||
append_dl(X-Y, [C | U]- U, X1-[]).
|
||||
|
||||
% "quick" append
|
||||
append_dl(X-Y, Y-Z, X-Z).
|
||||
8
Task/Matrix-transposition/Python/matrix-transposition.py
Normal file
8
Task/Matrix-transposition/Python/matrix-transposition.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
m=((1, 1, 1, 1),
|
||||
(2, 4, 8, 16),
|
||||
(3, 9, 27, 81),
|
||||
(4, 16, 64, 256),
|
||||
(5, 25,125, 625))
|
||||
print(zip(*m))
|
||||
# in Python 3.x, you would do:
|
||||
# print(list(zip(*m)))
|
||||
5
Task/Matrix-transposition/R/matrix-transposition.r
Normal file
5
Task/Matrix-transposition/R/matrix-transposition.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
b <- 1:5
|
||||
m <- matrix(c(b, b^2, b^3, b^4), 5, 4)
|
||||
print(m)
|
||||
tm <- t(m)
|
||||
print(tm)
|
||||
33
Task/Matrix-transposition/REXX/matrix-transposition.rexx
Normal file
33
Task/Matrix-transposition/REXX/matrix-transposition.rexx
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/*REXX program transposes a matrix, shows before and after matrixes. */
|
||||
x.=
|
||||
x.1='1.02 2.03 3.04 4.05 5.06 6.07 7.07'
|
||||
x.2='111 2222 33333 444444 5555555 66666666 777777777'
|
||||
|
||||
do r=1 while x.r\=='' /*build the "A" matric from X. numbers */
|
||||
do c=1 while x.r\==''
|
||||
parse var x.r a.r.c x.r
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
|
||||
rows=r-1; cols=c-1
|
||||
L=0 /*L is the maximum width element value.*/
|
||||
do i=1 for rows
|
||||
do j=1 for cols
|
||||
b.j.i = a.i.j; L=max(L,length(b.j.i))
|
||||
end /*j*/
|
||||
end /*i*/
|
||||
|
||||
call showMat 'A',rows,cols
|
||||
call showMat 'B',cols,rows
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────────SHOWMAT subroutine───────────────*/
|
||||
showMat: parse arg mat,rows,cols; say
|
||||
say center(mat 'matrix', cols*(L+1)+4, "─")
|
||||
|
||||
do r=1 for rows; _=
|
||||
do c=1 for cols;
|
||||
_=_ right(value(mat'.'r'.'c),L)
|
||||
end /*c*/
|
||||
say _
|
||||
end /*r*/
|
||||
return
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
(matrix-transpose (matrix [[1 2] [3 4]]))
|
||||
6
Task/Matrix-transposition/Ruby/matrix-transposition-1.rb
Normal file
6
Task/Matrix-transposition/Ruby/matrix-transposition-1.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
m=[[1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25,125, 625]]
|
||||
puts m.transpose
|
||||
8
Task/Matrix-transposition/Ruby/matrix-transposition-2.rb
Normal file
8
Task/Matrix-transposition/Ruby/matrix-transposition-2.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
require 'matrix'
|
||||
|
||||
m=Matrix[[1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25,125, 625]]
|
||||
puts m.transpose
|
||||
4
Task/Matrix-transposition/Ruby/matrix-transposition-3.rb
Normal file
4
Task/Matrix-transposition/Ruby/matrix-transposition-3.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def transpose(m)
|
||||
m[0].zip(*m[1..-1])
|
||||
end
|
||||
p transpose([[1,2,3],[4,5,6]])
|
||||
19
Task/Matrix-transposition/Scala/matrix-transposition.scala
Normal file
19
Task/Matrix-transposition/Scala/matrix-transposition.scala
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
scala> Array.tabulate(4)(i => Array.tabulate(4)(j => i*4 + j))
|
||||
res12: Array[Array[Int]] = Array(Array(0, 1, 2, 3), Array(4, 5, 6, 7), Array(8, 9, 10, 11), Array(12, 13, 14, 15))
|
||||
|
||||
scala> res12.transpose
|
||||
res13: Array[Array[Int]] = Array(Array(0, 4, 8, 12), Array(1, 5, 9, 13), Array(2, 6, 10, 14), Array(3, 7, 11, 15))
|
||||
|
||||
scala> res12 map (_ map ("%2d" format _) mkString " ") mkString "\n"
|
||||
res16: String =
|
||||
0 1 2 3
|
||||
4 5 6 7
|
||||
8 9 10 11
|
||||
12 13 14 15
|
||||
|
||||
scala> res13 map (_ map ("%2d" format _) mkString " ") mkString "\n"
|
||||
res17: String =
|
||||
0 4 8 12
|
||||
1 5 9 13
|
||||
2 6 10 14
|
||||
3 7 11 15
|
||||
2
Task/Matrix-transposition/Scheme/matrix-transposition.ss
Normal file
2
Task/Matrix-transposition/Scheme/matrix-transposition.ss
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(define (transpose m)
|
||||
(apply map list m))
|
||||
44
Task/Matrix-transposition/Tcl/matrix-transposition-1.tcl
Normal file
44
Task/Matrix-transposition/Tcl/matrix-transposition-1.tcl
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package require Tcl 8.5
|
||||
namespace path ::tcl::mathfunc
|
||||
|
||||
proc size {m} {
|
||||
set rows [llength $m]
|
||||
set cols [llength [lindex $m 0]]
|
||||
return [list $rows $cols]
|
||||
}
|
||||
proc transpose {m} {
|
||||
lassign [size $m] rows cols
|
||||
set new [lrepeat $cols [lrepeat $rows ""]]
|
||||
for {set i 0} {$i < $rows} {incr i} {
|
||||
for {set j 0} {$j < $cols} {incr j} {
|
||||
lset new $j $i [lindex $m $i $j]
|
||||
}
|
||||
}
|
||||
return $new
|
||||
}
|
||||
proc print_matrix {m {fmt "%.17g"}} {
|
||||
set max [widest $m $fmt]
|
||||
lassign [size $m] rows cols
|
||||
for {set i 0} {$i < $rows} {incr i} {
|
||||
for {set j 0} {$j < $cols} {incr j} {
|
||||
set s [format $fmt [lindex $m $i $j]]
|
||||
puts -nonewline [format "%*s " [lindex $max $j] $s]
|
||||
}
|
||||
puts ""
|
||||
}
|
||||
}
|
||||
proc widest {m {fmt "%.17g"}} {
|
||||
lassign [size $m] rows cols
|
||||
set max [lrepeat $cols 0]
|
||||
for {set i 0} {$i < $rows} {incr i} {
|
||||
for {set j 0} {$j < $cols} {incr j} {
|
||||
set s [format $fmt [lindex $m $i $j]]
|
||||
lset max $j [max [lindex $max $j] [string length $s]]
|
||||
}
|
||||
}
|
||||
return $max
|
||||
}
|
||||
|
||||
set m {{1 1 1 1} {2 4 8 16} {3 9 27 81} {4 16 64 256} {5 25 125 625}}
|
||||
print_matrix $m "%d"
|
||||
print_matrix [transpose $m] "%d"
|
||||
6
Task/Matrix-transposition/Tcl/matrix-transposition-2.tcl
Normal file
6
Task/Matrix-transposition/Tcl/matrix-transposition-2.tcl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package require struct::matrix
|
||||
struct::matrix M
|
||||
M deserialize {5 4 {{1 1 1 1} {2 4 8 16} {3 9 27 81} {4 16 64 256} {5 25 125 625}}}
|
||||
M format 2string
|
||||
M transpose
|
||||
M format 2string
|
||||
Loading…
Add table
Add a link
Reference in a new issue