I'm trying to use matrix algebra with the purpose of manipulating strings. This means being able to multiple matrix-like structures using concatenation and pasting of string or of arrays of strings.
I previously tried to implement the thing on R, but it was not possible as matrices can have only one dimensional entries.
I hope to be enough language-agnostic and abstract, but I'll use R-like code for sake of clarity. I should make explicit that I don't require real matrices, but matrix-like structure on which we can do matrices-like multiplication and retrieve the (ij) element of the structure.
{+,*} MATRICES MULTIPLICATION
The {+,*}-product of two square matrices A and B of dimension n is a matrix C defined by the elements: Ci,j = Sumk=1,...,nAi,k * Bk,j.
For example, consider the matrix M <- matrix(c(a,b,0,0,c,d,0,0,e),3,3)
. Then M times M is M <- matrix(c(a^2,a*b+b*c,b*d,0,c^2,c*d+d*e,0,0,e^2),3,3)
.
{c(,),paste0(,)} MATRICES MULTIPLICATION
The rule of this operation I would like to implement are the same of the previous stated multiplication with the essential mutation that the sum should be a concatenation and the product should be a paste. In other words, where in the previous formula we found a+b
, now the output should be "c(a,b)", and when we found a*b
, now we should read this as paste0(a,b)
.
Some of the usual properties have to be respected, namely the distributive properties and the 0 element properties. Hence, if a <- c("q",0,"w")
and b <- c("e")
then a*b <- c("qe",0,"we")
(and we should freely forget the 0 element, dropping it as it won't affect the computation.
Moreover, we are multiplying equal-dimensioned matrices, hence each element Ci,j = Sumk=1,...,nAi,k * Bk,j is now to be read as c("A[i,1]B[1,j]",...,"A[i,n]B[n,j]")
.
Finally, the outcome matrix-like structure should be something that we can use again for computations (so for example to make more complex computation as mult(mult(A,B),C) and so on...).
A SIMPLER CASE
For simplicity sake, let's start from the computation of product of the form mult(A,A)
, mult(mult(A,A),A)
and so on. We may also impose A to be a simple matrix, meaning that each of its elements are one dimensional string, and not concatenation of string.
Let's give an example. Let's be A a 3 dimensional matrix defined as A <- matrix(c("a","b",0,0,"c","d",0,0,"e"),3,3)
, then the multiplication of A times A should be mult(A,A) = matrix(c("aa",c("ab","bc"),"bd",0,"cc",c("cd","de"),0,0,"ee"),3,3)
and A3 should be mult(mult(A,A),A) = matrix(c("aaa",c("aab","abc","bcc"),c("abd","bcd","bde"),0,"ccc",c("ccd","cde","dee"),0,0,"eee"),3,3)
.
Question
How would you implement this? Which language seems more fitting?
c()
? What ispaste0()
? Why should anyone outside of the R community care? – n. 1.8e9-where's-my-share m.c(a,b,...,n)
returns an array with n elements a, b, ... n;paste0(abc,def)
, where abc and def are two strings, returns the stringabcdef
. The first function append a vector to another, the second paste together two or more strings. The code is written in R just because I was working in that language, but the question is a general one, regarding string manipulation under matrix algebra, which is useful in many problems (or may be) and I don't know any language doing that as builtin... – gvdrpaste0(c("a","b","c"),"l")
givesc("al","bl","cl")
which is similar to (a+b+c)*l=al+bl+cl... This is the kind of distributivity which I need. – gvdrpaste0(c("a","b","c"),c("1","2"))
. – n. 1.8e9-where's-my-share m.