Traccia matrice generalizzata


23

Ispirazione.

Dato (con ogni mezzo):

  • Una funzione black box a due argomenti (o singolo argomento costituito da un elenco di due elementi) , (input e output sono 1, 2, 3, ...)f: ℤ+ × ℤ+ → ℤ+
  • Una matrice intera strettamente positiva con almeno due righe e due colonne

restituisce la traccia della funzione della matrice .

Che cos'è una traccia di funzione ?

Una traccia matrice normale è la somma della diagonale maggiore (da sinistra in alto a destra in basso) di una matrice:

[[1,2,3],[4,5,6],[7,8,9]][1,5,9]1+5+915

Ma invece di sommare, vogliamo applicare flungo la diagonale:

[[1,2,3],[4,5,6],[7,8,9]][1,5,9]f(f(1,5),9)of(1,f(5,9))

Si prega di indicare se si utilizza da sinistra a destra o da destra a sinistra.

La matrice data e tutti i valori intermedi saranno numeri interi assolutamente positivi all'interno del dominio intero della tua lingua. La matrice può essere non quadrata.

Esempi

f(x,y) = xy, [[1,2,3],[4,5,6],[7,8,9]]1×5×945

f(x,y) = xy, [[1,2,3],[4,5,6],[7,8,9]]→ →1591

f(x,y) = x-y, [[4,5,6],[1,2,3]]4-22

f(x,y) = (x+y)⁄2, [[2,3,4],[5,6,7],[8,9,10]]5o7

f(x,y) = x+2y, [[1,2,3],[4,5,6],[7,8,9]]47o29

f(x,y) = max(x,y), [[1,2,3],[4,5,6],[7,8,9]]max(1,5,9)9

f(x,y) = 2x, [[1,2,3],[4,5,6],[7,8,9]]2o4

f(x,y) = lcm(x,y), [[2,2,2],[2,2,3],[2,3,3],[4,4,4]]lcm(2,2,3)6

Implementazione di riferimento.


Qual è la diagonale di [[2,2,2],[2,2,3],[2,3,3],[4,4,4]]?
totalmente umano il

3
@totallyhuman:[2,2,3]
Emigna

1
Accidenti, ho letto il titolo di "trance a matrice generalizzata" e sono rimasto molto deluso quando la pagina è stata caricata
tar

Risposte:


9

R , 40 30 byte

function(m,F)Reduce(F,diag(m))

Provalo online!

Verifica i casi di test.

Attraversa la diagonale, quindi da sinistra a destra in questo caso. Per gli operatori aritmetici, è possibile utilizzare "+"o backtick attorno agli operatori ( +,*,-,%/%,^,%%)

Abbastanza semplice: Reduceè R equivalente a fold, e la diagonale di una matrice è quegli elementi in a_ijcui i==j, cioè, dove gli indici rowe column sono gli stessi. diagha il comportamento appropriato per le matrici non quadrate.


8

Haskell , 39 byte

Grazie @Laikoni per avermi aiutato a risolvere la soluzione precedentemente non valida!

f!m=foldl1 f[h|h:_<-zipWith drop[0..]m]

Associati a sinistra, provalo online! (sostituisci foldl1con foldr1per associativo a destra)


che ne dici foldl1 f$zipWith(!!)m[0..]?
orgoglioso haskeller il

@proudhaskeller: Questo è quello che hanno già provato gli altri, ma questo fallisce nelle matrici non quadrate ..
ბიმო

5

Mathematica , 16 byte

-1 byte grazie a Martin Ender.

#~Tr~Fold[g]@*0&

Provalo online!

Soluzione alternativa, 17 byte

Fold[g]@*Diagonal

Provalo online!


17 byte (le funzioni della scatola nera possono essere assunte con un determinato nome)
Mr. Xcoder

Questa @*{}sintassi non ha molto senso (probabilmente intendevi @*List), ma il fatto che funzioni comunque è piuttosto interessante. In realtà, significa che è possibile sostituire il {}con a 0e salvare un byte.
Martin Ender,

@MartinEnder in realtà l'ho avuto Listprima, ma ho provato {}solo per il gusto di farlo e sono rimasto estremamente sorpreso dal fatto che funzionasse. Ha senso, ma come 0funziona? o0
totalmente umano il

1
@totallyhuman Allo stesso modo di {}. Attualmente stai usando {}come una funzione (o in realtà come una "testa" usando la terminologia di Mathematica). Se avessi usato un generico flì, avresti f[1,2,3](se quella è la diagonale). Ma con {}te ottieni {}[1,2,3]. È un'espressione completamente insignificante, ma le teste possono essere espressioni arbitrarie stesse e, se Mathematica non sa cosa farne, le lascia così come sono. La maggior parte delle funzioni di manipolazione dell'elenco di Mathematica in realtà funzionano con espressioni con una testa arbitraria e, nel caso di Fold, la testa viene semplicemente ignorata. [da confermare]
Martin Ender,

Quindi puoi usare 0come testa invece, che dà 0[1,2,3]che è ancora insignificante, ma funziona lo stesso.
Martin Ender,

4

Ottava , 61 57 53 byte

function m=g(f,m)for i=diag(m)'(2:end)m=f(m(1),i);end

Provalo online!

Definisce una funzione gche accetta un handle fe una matrice di funzioni m. Alla prima iterazione, m(1)restituisce l'elemento matrice in alto a sinistra; dopo ciò, ritorna e basta m.



@Giuseppe Questo è quello che ho fatto con la mia versione iniziale a 61 byte. Naturalmente, avrei dovuto combinare i punti di forza della mia versione a 57 e 61 byte, che in effetti dà una risposta a 53 byte. Grazie per avermelo fatto rivedere!
Sanchises,


3

Haskell , 47 45 42 byte

f%((h:t):r)|[]<-t*>r=h|x<-tail<$>r=f h$f%x

Provalo online! Definisce una funzione(%) che accetta una funzione e una matrice come elenco di elenchi come input.

La funzione è pieghe da destra a sinistra:

f % [[1,2,3], -> f 1 ( f % [[5,6],   -> f 1 ( f 5 ( f % [[9]] ) ) -> f 1 ( f 5 ( f 9 ) ) )
     [4,5,6],               [8,9]] )
     [7,8,9]]

f % ((h:t):r)              -- (h:t) is the first row and r the remaining rows
 | [] <- t *> r = h         -- a shorter way of checking wether t==[] or r==[]
 | x<-tail<$>r = f h $ f%x -- apply f to h and the result of recursively calling (%) on
                           -- the remaining matrix with the first column removed

Modifica: -2 byte grazie a BMO e -3 byte grazie a Zgarb !


1
43 byte usando $e semplificando il condizionale con *>.
Zgarb,

@Zgarb Bella idea da usare *>!
Laikoni,

3

APL (Dyalog Unicode) , 7 byte ( Adám's SBCS )

⎕/1 1⍉⎕

Provalo online!

-3 grazie a un suggerimento per convertirlo in un programma completo di Adám .

Da destra a sinistra.


Non c'è bisogno di usare l'SBCS di Adám qui: potresti semplicemente usare Dyalog Classic.
Zacharý,

@ Zacharý Il fatto è che sto rispondendo in Dyalog Unicode, Classic si sta deprecando nel tempo.
Erik the Outgolfer,

Non la tabella codici sebbene la tabella codici vivrà
Zacharý

@Zacharý Well, let's rather be consistent. :P
Erik the Outgolfer



2

Python 2, 61 bytes

lambda f,m:reduce(f,[l[i]for i,l in enumerate(m)if len(l)>i])

Try it online!

This works left-to-right.


@AsoneTuhid it can be any way, check the (x+y)⁄2 and x+2yexamples
Rod

Right, I misread, sorry
Asone Tuhid

2

JavaScript (ES6), 58 56 bytes

g=(f,a,r=a[i=0][0],e=a[++i]&&a[i][i])=>e?g(f,a,f(r,e)):r

Folds left-to-right. Edit: Saved 2 bytes by using the fact that the array is strictly positive. Alternate solution, also 56 bytes:

(f,a,g=r=>(e=a[++i]&&a[i][i])?g(f(r,e)):r)=>g(a[i=0][0])

It doesn't look like you need the 1/ and you can save another 2 bytes by moving some stuff around: f=>a=>(h=r=>(e=a[++i]&&a[i][i])?h(f(r,e)):r)(a[i=0][0]). TIO
Shaggy

@Shaggy Oh, it's strictly positive, I hadn't seen that.
Neil

Apparently we can assume that black-box functions are assigned to a predefined variable so you could save 2 bytes if you want to take advantage of that.
Shaggy

@Shaggy Actually I think it would save 4 bytes (2x f,) off the first version?
Neil

You're right; sorry, forgot to count the f, when calling g again.
Shaggy

2

JavaScript, 46 bytes

f=>a=>a.reduce((p,l,i)=>l[i]?f(p[0]|p,l[i]):p)

Thanks to @Shaggy, use bitwise or save one byte. That's magic.


This doesn't seem to work if the matrix has more rows than columns.
Shaggy

@Shaggy so sad, 47 bytes now...
tsh

Yeah, that's what I had originally, too. Was just about to edit the fix into my solution but you beat me too it :( I think you can get one byte back, though, by using bitwise OR, though.
Shaggy

@Shaggy so magic
tsh

Forgot to mention: Apparently we can assume that black-box functions are assigned to a predefined variable so you could save 3 bytes if you wanted to take advantage of that.
Shaggy

2

Java 8, 88 81 70 bytes

m->{int r=m[0][0],i=1;try{for(;;)r=f(r,m[i][i++]);}finally{return r;}}

Folds [[1,2,3],[4,5,6],[7,8,9]] to f(f(1,5),9).

-7 bytes indirectly thanks to @KamilDrakari by using a similar trick as he did in his C# answer: instead of having a maximum boundary for the loop based on the rows/columns, simply try-catch the ArrayIndexOutOfBoundsException.
-11 bytes replacing catch(Exception e) with finally.

Try it online.

Old 88 bytes answer:

m->{int r=m[0][0],i=1;for(;i<Math.min(m.length,m[0].length);)r=f(r,m[i][i++]);return r;}

Try it online.

Explanation:

m->{                   // Method with integer-matrix parameter and integer return-type
  int r=m[0][0],       //  Start the result at the integer of position 0,0 (0-indexed)
      i=1;             //  Start the index at 1 (0-indexed)
  try{for(;;)          //  Loop indefinitely
    r=f(r,m[i][i++]);} //   Call f with the result + next diagonal cell as arguments
                       //   (and increase `i` by 1 with `i++` afterwards)
  finally{             //  If an ArrayIndexOutOfBoundsException occurred we're done,
   return r;}}         //   in which case we return the result-integer

Black box input format:

Assumes a named function int f(int x,int y) is present, which is allowed according to this meta answer.

I have an abstract class Test containing the default function f(x,y), as well as the lambda above:

abstract class Test{
  int f(int x,int y){
    return x+y;
  }

  public java.util.function.Function<int[][],Integer>c=
    m->{int r=m[0][0],i=1;for(;i<Math.min(m.length,m[0].length);)r=f(r,m[i][i++]);return r;}
  ;
}

For the test cases, I overwrite this function f. For example, the first test case is called like this:

System.out.println(new Test(){
  @Override
  int f(int x,int y){
    return x*y;
  }
}.c.apply(new int[][]{
  new int[]{1,2,3},
  new int[]{4,5,6},
  new int[]{7,8,9}
}));

2

Attache, 14 bytes

Fold#/Diagonal

Try it online! Set to f and call as f[function, array].

Explanation

This is a fork of two functions: Fold and /Diagonal. This, for arguments f and a, is equivalent to:

Fold[f, (/Diagonal)[f, a]]

/, when applied monadically to a function, returns a function that is applied to its last argument. So, this is equivalent to:

Fold[f, Diagonal[a]]

This folds the function f over the main diagonal of a.


A home-brewed language which is readable‽
Adám

@Adám ;D yes indeed!
Conor O'Brien

2

AWK, 77 bytes

func z(F,M,r){for(e=1;e<M[1]&&e<M[2];)r=@F(r==""?M[1,1]:r,M[++e,e])
return r}

Try it online!

I was curious if AWK could do functional programming at all. I think this counts.

The "Matrix" is defined as a standard associative array, with extra fields M[1]=#rows and M[2]=#columns. The function name is passed in as a string which is evaluated via the @F(...) syntax. Evaluation is performed left to right. The r parameter is a placeholder to prevent overwriting an existing r variable and to avoid the need to reinitialize for each call. Typically extra space is added to designate such placeholders in AWK, but this is code golf, so every byte counts. :)

The TIO link implements all the test cases.


2

05AB1E, 15 10 bytes

Folds from right-to-left
Saved 5 bytes using a new built-in as suggested by Kevin Cruijssen

Å\`[.g#I.V

Explanation

Works the same as the old version, except that Å\ is a new built-in for pushing the main diagonal.

Try it online! or as a Test Suite

Old Version

¬g£vyNè}[.g#I.V

Try it online! or as a Test suite

Explanation

¬                 # get the head of the input (first row)
 g                # get its length (number of columns)
  £               # take that many rows from input
   v              # for each row_index, row (N,y) do:
    y             # push the row
     Nè           # get the nth element of the row
       }          # end loop
        [.g#      # loop until one value remain on the stack
            I.V   # run the input function

1
¬g£vyNè}[ can be Å\`[ now, saving 5 bytes.
Kevin Cruijssen

1

Husk, 7 bytes

Thanks @Zgarb for fixing my submission!

Ḟ₁§z!Tŀ

Associates to the left, Try it online! (for a right-associative version simply replace by F)

Explanation

Unfortunately there's no easy way to get the diagonal of a matrix, so most the bytes are for that:

Ḟ₁§z!Tŀ  -- function ₁ is the function and matrix A implicit, example: 
  §      -- fork A
     T   -- | transpose A: [[1,4,7],[2,5,8],[3,6,9]]
      ŀ  -- | enumerate A: [1,2,3]
   z!    -- and zipWith index: [1,5,9]
Ḟ₁       -- right fold function

Huh, built-in for anti-diagonals, but not for diagonals‽
Adám

2
@Adám I assume that's because you can compute antidiagonals of infinite matrices but not diagonals.
Martin Ender

1

SNOBOL4 (CSNOBOL4), 86 bytes

T	I =1
	T =M<1,1>
I	I =I + 1
	T =EVAL(F '(T,M<I,I>)')	:S(I)F(RETURN)
	DEFINE('T(M,F)')

Try it online!

Defines a function T (for TRACE) that takes an ARRAY and a string F that's the name of a function. Folds left-to-right.

Using indirect reference ($) doesn't work with functions. So using EVAL and passing a string to the name seems to be the only way to get a black-box function in SNOBOL.

Also, it's quite painful to define arrays; however, because invalid array references cause FAILURE, this works for non-square arrays -- if I is out-of-bounds in either dimension, the F(RETURN) forces the function to return.

Edit:

Possibly, based on this meta post, I may assume that the black-box function F is defined under the name F, which would drop this to 75 bytes (remove use of EVAL and ,F in the function definition). However, I prefer this version since it's closer to passing a reference to a function.



1

tinylisp, 79 bytes

(load library
(d D(q((M)(i(h M)(c(h(h M))(D(map t(t M))))(
(q((F M)(foldl F(D M

The last line is an unnamed lambda function that takes a function and matrix and returns the matrix trace. The trace is left-associative (i.e. f(f(1,5),9)). Try it online!

Ungolfed

We define a helper function to compute the diagonal; then generalized-trace is merely a small wrapper around the library function foldl.

(load library)

(def diagonal
 (lambda (matrix)
  (if (head matrix)
   (cons
    (head (head matrix))
    (diagonal (map tail (tail matrix))))
   nil)))

(def generalized-trace
 (lambda (func matrix)
  (foldl func (diagonal matrix))))

When computing the diagonal recursively, we check whether (head matrix) is truthy. If the matrix is out of rows, it will be the empty list (nil), and head of nil is nil--falsey. Or, if the matrix is out of columns, its first row (head) will be the empty list (nil)--falsey. Otherwise, there will be a nonempty first row, which is truthy.

So, if the first row doesn't exist or is empty, we return nil. Otherwise, if there is a nonempty first row, we take (head (head matrix))--the first element of the first row--and cons (prepend) it to the result of the recursive call. The argument to the recursive call is (map tail (tail matrix))--that is, take all rows but the first, and take all but the first element of each row.



1

C# (Visual C# Compiler), 72 69 60 bytes

m=>{try{for(int i=1;;m[0][0]=f(m[0][0],m[i][i++]));}catch{}}

Try it online!

try/catch allows the diagonal to be correctly reached by simply going along it and terminating when out of bounds.

3 bytes saved because, as pointed out by Kevin Cruijssen, black-box functions can be assumed to exist under a specific name.

9 bytes saved by returning via modifying an argument.

Thus, the function is called by storing the desired function under the name f, calling trace(matrix), and the result is stored in matrix[0][0].

Alternatively, if you really like verbosity,

C# (Visual C# Compiler), 97 + 13 = 110 78 69 bytes

(int[][]m)=>{try{for(int i=1;;m[0][0]=f(m[0][0],m[i][i++]));}catch{}}

Try it online!

32 bytes saved by using a predefined function, because not taking the function as a parameter allowed removing the System import and the long Func generic type.


Nice trick with the try-catch. I've been able to golf 7 bytes on my Java 8 answer (even though I have to use catch(Exception e) instead of catch. :) EDIT: Oh, been able to replace the catch(Exception e) with finally to save more bytes. Thanks again. +1 from me.
Kevin Cruijssen

@KevinCruijssen you may also be able to benefit from my newest improvement (though I don't remember for sure whether Java is amenable to modifying arguments)
Kamil Drakari

Thanks for letting me know. Although it is possible in Java, it means I'll have to change the finally into catch(Exception e), because I'm not returning inside the finally anymore. So m->{try{for(int i=1;;m[0][0]=f(m[0][0],m[i][i++]));}catch(Exception e){}} (73 bytes) is unfortunately longer for me in comparison to my current answer m->{int r=m[0][0],i=1;try{for(;;)r=f(r,m[i][i++]);}finally{return r;}} (70 bytes) But indeed a nice way to save bytes in your answer! :) Too bad I can only +1 your answer once.
Kevin Cruijssen

1

JavaScript, 61 57 56 52 50 44 42 bytes

Reduces left to right. Assumes the function is assigned to variable f, as per this meta post brought to my attention by Mr. Xcoder & totallyhuman. Can't say as I agree with it as it directly contradicts our existing consensus that we may not assume input is assigned to a pre-defined variable, but I'll take the few bytes saving for now.

a=>a.map((y,z)=>x=(n=y[z])?z?f(x,n):n:x)|x

Test Cases

g=
a=>a.map((y,z)=>x=(n=y[z])?z?f(x,n):n:x)|x
o.innerHTML=[[`f(x,y) = xy`,[[1,2,3],[4,5,6],[7,8,9]],(x,y)=>x*y,45],[`f(x,y) = x<sup>y</sup>`,[[1,2,3],[4,5,6],[7,8,9]],(x,y)=>x**y,1],[`f(x,y) = x-y`,[[4,5,6],[1,2,3]],(x,y)=>x-y,2],[`f(x,y) = <sup>(x+y)</sup>⁄<sub>2</sub>`,[[2,3,4],[5,6,7],[8,9,10]],(x,y)=>(x+y)/2,7],[`f(x,y) = x+2y`,[[1,2,3],[4,5,6],[7,8,9]],(x,y)=>x+2*y,29],[`f(x,y) = max(x,y)`,[[1,2,3],[4,5,6],[7,8,9]],(x,y)=>Math.max(x,y),9],[`f(x,y) = 2x`,[[1,2,3],[4,5,6],[7,8,9]],(x,y)=>2*x,4],[`f(x,y) = lcm(x,y)`,[[2,2,2],[2,2,3],[2,3,3],[4,4,4]],(x,y)=>-~[...Array(x*y).keys()].find(z=>!(++z%x|z%y)),6]].map(([a,b,c,d],e)=>`Test #${++e}:  ${a}\nMatrix:   ${JSON.stringify(b)}\nFunction: ${f=c}\nResult:   ${g(b)}\nExpected: ${d}`).join`\n\n`
<pre id=o></pre>


1

APL NARS, 20 bytes, 10 chars

{⍺⍺/1 1⍉⍵}

test:

  f←{⍺⍺/1 1⍉⍵}
  ⎕←q←3 3⍴⍳10    
1 2 3
4 5 6
7 8 9
  ×f q
45
  *f q
1
  {⍺+2×⍵}f q
47
  ⌈f q
9
  {2×⍺+0×⍵}f q
2
  -f ⊃(4 5 6)(1 2 3)
2
  {(⍺+⍵)÷2}f ⊃(2 3 4)(5 6 7)(8 9 10)
5
  ∧f ⊃(2 2 2)(2 2 3)(2 3 3)(4 4 4)
6

Good job. While I think you arrive to this on you own, it happens to be identical to Erik the Outgolfer's original solution.
Adám

0

Jelly, 5 bytes

Left-to-right.

ŒDḢç/

Try it online!

Disclaimer: I do not know if this an acceptable input method for black-box functions. This assumes that the function is implemented in the link above, and is thus "named" (that is, it's callable with) ç, but otherwise I have no way to assign it to ç. If anyone has more experience with Jelly + black box functions, I would appreciate thoughts. After spending some time in chat, we figured that using ç might indeed be valid.


0

Clojure, 30 bytes

#(reduce %2(map nth %(range)))

Reduces "from the left".


Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.