September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,14 +1,14 @@
# syntax: GAWK -f MATRIX_TRANSPOSITION.AWK filename
{ if (NF > nf) {
nf = NF
nf = NF
}
for (i=1; i<=nf; i++) {
row[i] = row[i] $i " "
row[i] = row[i] $i " "
}
}
END {
for (i=1; i<=nf; i++) {
printf("%s\n",row[i])
printf("%s\n",row[i])
}
exit(0)
}

View file

@ -0,0 +1,29 @@
# Usage: GAWK -f MATRIX_TRANSPOSITION.AWK filename
{
i = NR
for (j = 1; j <= NF; j++) {
a[i,j] = $j
}
ranka1 = i
ranka2 = max(ranka2, NF)
}
END {
rankb1 = ranka2
rankb2 = ranka1
b[rankb1, rankb2] = 0
transpose_matrix(b, a)
for (i = 1; i <= rankb1; i++) {
for (j = 1; j <= rankb2; j++) {
printf("%g%c", b[i,j], j < rankb2 ? " " : "\n");
}
}
}
function transpose_matrix(target, source, key, idx) {
for (key in source) {
split(key, idx, SUBSEP)
target[idx[2], idx[1]] = source[idx[1], idx[2]]
}
}
function max(m, n) {
return m > n ? m : n
}

View file

@ -0,0 +1,5 @@
(defun transpose (m)
(apply #'mapcar* #'list m))
;;test for transposition function
(transpose '((2 3 4 5) (3 5 6 9) (9 9 9 9)))

View file

@ -0,0 +1,18 @@
-module(transmatrix).
-export([trans/1,transL/1]).
% using built-ins hd = head, tl = tail
trans([[]|_]) -> [];
trans(M) ->
[ lists:map(fun hd/1, M) | transpose( lists:map(fun tl/1, M) ) ].
% Purist version
transL( [ [Elem | Rest] | List] ) ->
[ [Elem | [H || [H | _] <- List] ] |
transL( [Rest |
[ T || [_ | T] <- List ] ]
) ];
transL([ [] | List] ) -> transL(List);
transL([]) -> [].

View file

@ -3,12 +3,15 @@
// transpose :: [[a]] -> [[a]]
const transpose = xs =>
xs[0].map((_, iCol) => xs.map((row) => row[iCol]));
xs[0].map((_, iCol) => xs.map(row => row[iCol]));
// TEST
return transpose([
[1, 2],
[3, 4],
[5, 6]
]);
// TEST -----------------------------------------------
return(
transpose([
[1, 2],
[3, 4],
[5, 6]
])
);
})();

View file

@ -4,7 +4,7 @@ julia> [1 2 3 ; 4 5 6] # a 2x3 matrix
4 5 6
julia> [1 2 3 ; 4 5 6]' # note the quote
3x2 Array{Int64,2}:
3x2 LinearAlgebra.Adjoint{Int64,Array{Int64,2}}:
1 4
2 5
3 6

View file

@ -0,0 +1,3 @@
function transpose($m) {
return count($m) == 0 ? $m : (count($m) == 1 ? array_chunk($m[0], 1) : array_map(null, ...$m));
}

View file

@ -0,0 +1,54 @@
#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
'----------------------------------------------------------------------
SUB TransposeMatrix(InitMatrix() AS DWORD, TransposedMatrix() AS DWORD)
LOCAL l1, l2, u1, u2 AS LONG
l1 = LBOUND(InitMatrix, 1)
l2 = LBOUND(InitMatrix, 2)
u1 = UBOUND(InitMatrix, 1)
u2 = UBOUND(InitMatrix, 2)
REDIM TransposedMatrix(l2 TO u2, l1 TO u1)
MAT TransposedMatrix() = TRN(InitMatrix())
END SUB
'----------------------------------------------------------------------
SUB PrintMatrix(a() AS DWORD)
LOCAL l1, l2, u1, u2, r, c AS LONG
LOCAL s AS STRING * 8
l1 = LBOUND(a(), 1)
l2 = LBOUND(a(), 2)
u1 = UBOUND(a(), 1)
u2 = UBOUND(a(), 2)
FOR r = l1 TO u1
FOR c = l2 TO u2
RSET s = STR$(a(r, c))
CON.PRINT s;
NEXT c
CON.PRINT
NEXT r
END SUB
'----------------------------------------------------------------------
SUB TranspositionDemo(BYVAL DimSize1 AS DWORD, BYVAL DimSize2 AS DWORD)
LOCAL r, c, cc AS DWORD
LOCAL a() AS DWORD
LOCAL b() AS DWORD
cc = DimSize2
DECR DimSize1
DECR DimSize2
REDIM a(0 TO DimSize1, 0 TO DimSize2)
FOR r = 0 TO DimSize1
FOR c = 0 TO DimSize2
a(r, c)= (cc * r) + c + 1
NEXT c
NEXT r
CON.PRINT "initial matrix:"
PrintMatrix a()
TransposeMatrix a(), b()
CON.PRINT "transposed matrix:"
PrintMatrix b()
END SUB
'----------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
TranspositionDemo 3, 3
TranspositionDemo 3, 7
END FUNCTION

View file

@ -0,0 +1,41 @@
# transpose :: Matrix a -> Matrix a
def transpose(m):
if m:
inner = type(m[0])
z = zip(*m)
return (type(m))(
map(inner, z) if tuple != inner else z
)
else:
return m
if __name__ == '__main__':
# TRANSPOSING FOUR BASIC TYPES OF PYTHON MATRIX
# Cartesian product of (Outer, Inner) with (List, Tuple)
# Matrix any = Tuple of Tuples of any type
tts = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
# Matrix any = Tuple of Lists of any type
tls = ([1, 2, 3], [4, 5, 6], [7, 8, 9])
emptyTuple = ()
# Matrix any = List of Lists of any type
lls = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Matrix any = List of Tuples of any type
lts = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
emptyList = []
print('transpose function :: (Transposition without type change):\n')
for m in [emptyTuple, tts, tls, emptyList, lls, lts]:
tm = transpose(m)
print (
type(tm).__name__ + (
(' of ' + type(tm[0]).__name__) if m else ''
) + ' :: ' + str(m) + ' -> ' + str(tm)
)

View file

@ -0,0 +1,8 @@
# Uneven list of lists
uls = [[10, 11], [20], [], [30, 31, 32]]
print (
list(zip(*uls))
)
# --> []

View file

@ -0,0 +1,130 @@
'''Transposition of row sets with possible gaps'''
from collections import defaultdict
# listTranspose :: [[a]] -> [[a]]
def listTranspose(xss):
'''Transposition of a matrix which may
contain gaps.
'''
def go(xss):
if xss:
h, *t = xss
return (
[[h[0]] + [xs[0] for xs in t if xs]] + (
go([h[1:]] + [xs[1:] for xs in t])
)
) if h and isinstance(h, list) else go(t)
else:
return []
return go(xss)
# TEST ----------------------------------------------------
# main :: IO ()
def main():
'''Tests with various lists of rows or non-row data.'''
def labelledList(kxs):
k, xs = kxs
return k + ': ' + showList(xs)
print(
fTable(
__doc__ + ':\n'
)(labelledList)(fmapFn(showList)(snd))(
fmapTuple(listTranspose)
)([
('Square', [[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
('Rectangle', [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]),
('Rows with gaps', [[10, 11], [20], [], [31, 32, 33]]),
('Single row', [[1, 2, 3]]),
('Single row, one cell', [[1]]),
('Not rows', [1, 2, 3]),
('Nothing', [])
])
)
# TEST RESULT FORMATTING ----------------------------------
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
# fmapFn :: (a -> b) -> (r -> a) -> r -> b
def fmapFn(f):
'''The application of f to the result of g.
fmap over a function is composition.
'''
return lambda g: lambda x: f(g(x))
# fmapTuple :: (a -> b) -> (c, a) -> (c, b)
def fmapTuple(f):
'''A pair in which f has been
applied to the second item.
'''
return lambda ab: (ab[0], f(ab[1])) if (
2 == len(ab)
) else None
# show :: a -> String
def show(x):
'''Stringification of a value.'''
def go(v):
return defaultdict(lambda: repr, [
('list', showList)
# ('Either', showLR),
# ('Maybe', showMaybe),
# ('Tree', drawTree)
])[
typeName(v)
](v)
return go(x)
# showList :: [a] -> String
def showList(xs):
'''Stringification of a list.'''
return '[' + ','.join(show(x) for x in xs) + ']'
# snd :: (a, b) -> b
def snd(tpl):
'''Second member of a pair.'''
return tpl[1]
# typeName :: a -> String
def typeName(x):
'''Name string for a built-in or user-defined type.
Selector for type-specific instances
of polymorphic functions.
'''
if isinstance(x, dict):
return x.get('type') if 'type' in x else 'dict'
else:
return 'iter' if hasattr(x, '__next__') else (
type(x).__name__
)
# MAIN ---
if __name__ == '__main__':
main()

View file

@ -0,0 +1,25 @@
fn main() {
let m = vec![vec![1, 2, 3], vec![4, 5, 6]];
println!("Matrix:\n{}", matrix_to_string(&m));
let t = matrix_transpose(m);
println!("Transpose:\n{}", matrix_to_string(&t));
}
fn matrix_to_string(m: &Vec<Vec<i32>>) -> String {
m.iter().fold("".to_string(), |a, r| {
a + &r
.iter()
.fold("".to_string(), |b, e| b + "\t" + &e.to_string())
+ "\n"
})
}
fn matrix_transpose(m: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut t = vec![Vec::with_capacity(m.len()); m[0].len()];
for r in m {
for i in 0..r.len() {
t[i].push(r[i]);
}
}
t
}

View file

@ -0,0 +1,3 @@
Function transpose(m As Variant) As Variant
transpose = WorksheetFunction.transpose(m)
End Function

View file

@ -0,0 +1,56 @@
Option Explicit
'----------------------------------------------------------------------
Function TransposeMatrix(InitMatrix() As Long, TransposedMatrix() As Long)
Dim l1 As Long, l2 As Long, u1 As Long, u2 As Long, r As Long, c As Long
l1 = LBound(InitMatrix, 1)
l2 = LBound(InitMatrix, 2)
u1 = UBound(InitMatrix, 1)
u2 = UBound(InitMatrix, 2)
ReDim TransposedMatrix(l2 To u2, l1 To u1)
For r = l1 To u1
For c = l2 To u2
TransposedMatrix(c, r) = InitMatrix(r, c)
Next c
Next r
End Function
'----------------------------------------------------------------------
Sub PrintMatrix(a() As Long)
Dim l1 As Long, l2 As Long, u1 As Long, u2 As Long, r As Long, c As Long
Dim s As String * 8
l1 = LBound(a(), 1)
l2 = LBound(a(), 2)
u1 = UBound(a(), 1)
u2 = UBound(a(), 2)
For r = l1 To u1
For c = l2 To u2
RSet s = Str$(a(r, c))
Debug.Print s;
Next c
Debug.Print
Next r
End Sub
'----------------------------------------------------------------------
Sub TranspositionDemo(ByVal DimSize1 As Long, ByVal DimSize2 As Long)
Dim r, c, cc As Long
Dim a() As Long
Dim b() As Long
cc = DimSize2
DimSize1 = DimSize1 - 1
DimSize2 = DimSize2 - 1
ReDim a(0 To DimSize1, 0 To DimSize2)
For r = 0 To DimSize1
For c = 0 To DimSize2
a(r, c) = (cc * r) + c + 1
Next c
Next r
Debug.Print "initial matrix:"
PrintMatrix a()
TransposeMatrix a(), b()
Debug.Print "transposed matrix:"
PrintMatrix b()
End Sub
'----------------------------------------------------------------------
Sub Main()
TranspositionDemo 3, 3
TranspositionDemo 3, 7
End Sub