# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Efficiently replace rectangular submatrices of an input data matrix

Contributed by:
Bradley Klee

ResourceFunction["SubmatrixReplace"][ applies |

Each rule in *rules* must map between submatrices of fixed rectangular dimensions *m*×*n*, but *m* and *n* may vary across *rules*.

The replacement procedure groups *rules* by submatrix dimension, and applies them to individual submatrices using ReplaceAll.

When application of *rules* over all submatrices leads to two or more plausible image values for one particular matrix element, a *collision *is said to occur.

Collisions occur in two different cases:

Applying only one replacement rule may cause a collision within a region where two submatrices overlap.

Applying many distinct replacement rules may cause a collision within a region where two submatrices overlap.

Collisions do not occur in only one submatrix, because the underlying mechanism involves ReplaceAll.

ResourceFunction["SubmatrixReplace"] takes the following options:

"Cyclic" | {True,True} | determines cyclic or terminal boundary conditions for row and columns |

CollisionFunction | First[#2]& | determines how to resolve collisions |

Method | "Structured" | specifies how to handle submatrices |

The CollisionFunction acts locally on elements by returning an output value from three sequential inputs:

#1 | a single initial value |

#2 | a list containing the stack of candidate replacement values |

#3 | a list containing row and column indices |

Choosing Method "Structured" uses the resource function BlockSubmatrices to build an optimized data structure.

Replace the central diagonal of an identity matrix:

In[1]:= |

Out[1]= |

Use a functional notation to trim the output of DiamondMatrix:

In[2]:= |

Out[2]= |

Notice collision stacking when highlighting pattern fragments within a HadamardMatrix output:

In[3]:= |

Out[3]= |

Change resolution of collisions by setting the CollisionFunction option:

In[4]:= |

Out[4]= |

Test occurrence of certain blocks in a pseudorandom matrix pattern:

In[5]:= |

Out[5]= |

Restrict search range to a central patch:

In[6]:= |

Out[6]= |

Timing test Method Simple against the task of highlighting Rule 110's periodic background:

In[7]:= |

Out[7]= |

Repeat timing test with default Method Structured:

In[8]:= |

Out[8]= |

Compare complex-valued collision alternatives by inspecting elements:

In[9]:= |

Out[9]= |

Even using only one rule, a collision can still occur:

In[10]:= |

Out[10]= |

Highlight persistent structures in a 2D Cellular Automaton output:

In[11]:= |

Out[11]= |

- 1.0.0 – 18 March 2022

This work is licensed under a Creative Commons Attribution 4.0 International License