TransWikia.com

Elegant way to construct a matrix with symbolic entries

Mathematica Asked on February 6, 2021

I would like to construct a matrix $G$ composed of block matrices $G^{(i)}$ as defined entry-wise below stacked vertically as
$$G = begin{bmatrix} G^{(1)} G^{(2)} vdots G^{(i)} vdots G^{(n)} end{bmatrix}$$
for some natural number $n$.

$G^{(i)}_{j,k}$ stands for the element of the $j$‘th row and the $k$‘th column of the $i$‘th block matrix.
begin{align}
G^{(i)}_{j,k} :=& frac{t_{i+1}-t}{h_i}delta_{k,i}+frac{t-t_i}{h_i}delta_{k,i+1} -frac16(t-t_i)(t_{i+1}-t)Big(1+frac{t_{i+1}-t}{h_i}Big)delta_{k,n+i}
&-frac16(t-t_i)(t_{i+1}-t)Big(1+frac{t-t_i}{h_i}Big)delta_{k,n+i+1} Bigg|_{t=t_{j,i}}
end{align}

where $iin{1,2,cdots,n}$, $jin{1,2,cdots,m(j)}$, $m(j)$ is a natural numbered function of $j$, and $kin{1,2,cdots,2n-2}$, $delta_{i,j}$ is the Kronecker delta function, and the vertical bar at the right end indicates $t$ is to be evaluated at $t_{j,t}$.

What is the most elegant and convenient way to do this?

2 Answers

Assuming you have definitions already for n, single argument t[x], and two-argument t[x,y], then this is fairly straightforward:

g[i_, n_] := 
 Table[(t[i + 1] - t[j, i])/h[i] KroneckerDelta[k, 
     i] + (t[j, i] - t[i])/h[i] KroneckerDelta[k, i + 1] - 
   1/6 (t[j, i] - t[i]) (t[i + 1] - 
      t[j, i]) (1 + (t[i + 1] - t[j, i])/h[i]) KroneckerDelta[k, 
     n + i] - 
   1/6 (t[j, i] - t[i]) (t[i + 1] - 
      t[j, i]) (1 + ((t[j, i] - t[i])/h[i])) KroneckerDelta[k, 
     n + i + 1], {j, 1, 3}, {k, 1, 2*n-2}]

result = With[{n = 4}, Join @@ Array[g[#, n] &, n]]
result // Dimensions (*expected {12,3}*)
result // MatrixForm

Answered by flinty on February 6, 2021

Is this a formula with the Einstein sum convention. If so then the summation over two same indices has to be carried through.

g[i_, j_, k_, n_] := 
 Sum[(t[i + 1] - t[j, i])/h[ii] KroneckerDelta[k, 
     ii] + (t[j, i] - t[ii])/h[ii] KroneckerDelta[k, ii + 1] - 
   1/6 (t[j, i] - t[ii]) (t[ii + 1] - 
      t[j, i]) (1 + (t[ii + 1] - t[j, i])/h[ii]) KroneckerDelta[n + k,
      n + ii] - 
   1/6 (t[j, i] - t[ii]) (t[ii + 1] - 
      t[j, i]) (1 + ((t[j, i] - t[ii])/h[ii])) KroneckerDelta[n + k, 
     n + ii + 1], {ii, 1, 3}]

g[1, j, k, n]

(KroneckerDelta[1, k] (t[2] - t[j, 1]))/h[1] + (
 KroneckerDelta[2, k] (t[2] - t[j, 1]))/h[2] + (
 KroneckerDelta[3, k] (t[2] - t[j, 1]))/h[3] + (
 KroneckerDelta[2, k] (-t[1] + t[j, 1]))/h[1] - 
 1/6 KroneckerDelta[1 + n, 
   k + n] (1 + (t[2] - t[j, 1])/h[1]) (t[2] - t[j, 1]) (-t[1] + 
    t[j, 1]) + (KroneckerDelta[3, k] (-t[2] + t[j, 1]))/h[2] - 
 1/6 KroneckerDelta[2 + n, 
   k + n] (1 + (t[3] - t[j, 1])/h[2]) (t[3] - t[j, 1]) (-t[2] + 
    t[j, 1]) + (KroneckerDelta[4, k] (-t[3] + t[j, 1]))/h[3] - 
 1/6 KroneckerDelta[3 + n, 
   k + n] (1 + (t[4] - t[j, 1])/h[3]) (t[4] - t[j, 1]) (-t[3] + 
    t[j, 1]) - 
 1/6 KroneckerDelta[2 + n, 
   k + n] (t[2] - t[j, 1]) (-t[1] + t[j, 1]) (1 + (-t[1] + t[j, 1])/
    h[1]) - 1/
  6 KroneckerDelta[3 + n, 
   k + n] (t[3] - t[j, 1]) (-t[2] + t[j, 1]) (1 + (-t[2] + t[j, 1])/
    h[2]) - 1/
  6 KroneckerDelta[4 + n, 
   k + n] (t[4] - t[j, 1]) (-t[3] + t[j, 1]) (1 + (-t[3] + t[j, 1])/
    h[3])

The interpretation distinguishes between the indices i of the Einstein summation and the indices i from the discretization of time i in t_ij.

Example for displaying this as a matrix is

Table[h[i, j, k, n], {i, 1, 3}] // MatrixForm

enter image description here

This is much to lengthy for g.

Answered by Steffen Jaeschke on February 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP