|
|
@ -24,48 +24,47 @@
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Define MatrixSymbols with specific sizes\n",
|
|
|
|
"# Define MatrixSymbols with specific sizes\n",
|
|
|
|
"# A = 2x2, B = 3x3, R = 4x4\n",
|
|
|
|
"# A = 2x2, B = 3x3, R = 4x4\n",
|
|
|
|
"AA = MatrixSymbol('AA', 2, 2) # 2x2\n",
|
|
|
|
"AA = MatrixSymbol(\"AA\", 2, 2) # 2x2\n",
|
|
|
|
"AB = MatrixSymbol('AB', 2, 3) # 2x3\n",
|
|
|
|
"AB = MatrixSymbol(\"AB\", 2, 3) # 2x3\n",
|
|
|
|
"BA = MatrixSymbol('BA', 3, 2) # 3x2\n",
|
|
|
|
"BA = MatrixSymbol(\"BA\", 3, 2) # 3x2\n",
|
|
|
|
"AR = MatrixSymbol('AR', 2, 4) # 2x4\n",
|
|
|
|
"AR = MatrixSymbol(\"AR\", 2, 4) # 2x4\n",
|
|
|
|
"RA = MatrixSymbol('RA', 4, 2) # 4x2\n",
|
|
|
|
"RA = MatrixSymbol(\"RA\", 4, 2) # 4x2\n",
|
|
|
|
"BB = MatrixSymbol('BB', 3, 3) # 3x3\n",
|
|
|
|
"BB = MatrixSymbol(\"BB\", 3, 3) # 3x3\n",
|
|
|
|
"BR = MatrixSymbol('BR', 3, 4) # 3x4\n",
|
|
|
|
"BR = MatrixSymbol(\"BR\", 3, 4) # 3x4\n",
|
|
|
|
"RB = MatrixSymbol('RB', 4, 3) # 4x3\n",
|
|
|
|
"RB = MatrixSymbol(\"RB\", 4, 3) # 4x3\n",
|
|
|
|
"RR = MatrixSymbol('RR', 4, 4) # 4x4\n",
|
|
|
|
"RR = MatrixSymbol(\"RR\", 4, 4) # 4x4\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Create block matrices VA and VB using BlockMatrix\n",
|
|
|
|
"# Create block matrices VA and VB using BlockMatrix\n",
|
|
|
|
"# We need to use ZeroMatrix with appropriate dimensions for padding\n",
|
|
|
|
"# We need to use ZeroMatrix with appropriate dimensions for padding\n",
|
|
|
|
"VA = BlockMatrix([\n",
|
|
|
|
"VA = BlockMatrix(\n",
|
|
|
|
|
|
|
|
" [\n",
|
|
|
|
" [AA, 0.5 * AB, 0.5 * AR],\n",
|
|
|
|
" [AA, 0.5 * AB, 0.5 * AR],\n",
|
|
|
|
" [0.5 * BA, ZeroMatrix(3, 3), ZeroMatrix(3, 4)],\n",
|
|
|
|
" [0.5 * BA, ZeroMatrix(3, 3), ZeroMatrix(3, 4)],\n",
|
|
|
|
" [0.5*RA, ZeroMatrix(4,3), ZeroMatrix(4,4)]\n",
|
|
|
|
" [0.5 * RA, ZeroMatrix(4, 3), ZeroMatrix(4, 4)],\n",
|
|
|
|
"])\n",
|
|
|
|
" ]\n",
|
|
|
|
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"VB = BlockMatrix([\n",
|
|
|
|
"VB = BlockMatrix(\n",
|
|
|
|
|
|
|
|
" [\n",
|
|
|
|
" [ZeroMatrix(2, 2), 0.5 * AB, ZeroMatrix(2, 4)],\n",
|
|
|
|
" [ZeroMatrix(2, 2), 0.5 * AB, ZeroMatrix(2, 4)],\n",
|
|
|
|
" [0.5 * BA, BB, 0.5 * BR],\n",
|
|
|
|
" [0.5 * BA, BB, 0.5 * BR],\n",
|
|
|
|
" [ZeroMatrix(4,2), 0.5*RB, ZeroMatrix(4,4)]\n",
|
|
|
|
" [ZeroMatrix(4, 2), 0.5 * RB, ZeroMatrix(4, 4)],\n",
|
|
|
|
"])\n",
|
|
|
|
" ]\n",
|
|
|
|
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Define G matrix symbols with matching dimensions\n",
|
|
|
|
"# Define G matrix symbols with matching dimensions\n",
|
|
|
|
"G_AA = MatrixSymbol('G_AA', 2, 2)\n",
|
|
|
|
"G_AA = MatrixSymbol(\"G_AA\", 2, 2)\n",
|
|
|
|
"G_AB = MatrixSymbol('G_AB', 2, 3)\n",
|
|
|
|
"G_AB = MatrixSymbol(\"G_AB\", 2, 3)\n",
|
|
|
|
"G_BA = MatrixSymbol('G_BA', 3, 2)\n",
|
|
|
|
"G_BA = MatrixSymbol(\"G_BA\", 3, 2)\n",
|
|
|
|
"G_AR = MatrixSymbol('G_AR', 2, 4)\n",
|
|
|
|
"G_AR = MatrixSymbol(\"G_AR\", 2, 4)\n",
|
|
|
|
"G_RA = MatrixSymbol('G_RA', 4, 2)\n",
|
|
|
|
"G_RA = MatrixSymbol(\"G_RA\", 4, 2)\n",
|
|
|
|
"G_BB = MatrixSymbol('G_BB', 3, 3)\n",
|
|
|
|
"G_BB = MatrixSymbol(\"G_BB\", 3, 3)\n",
|
|
|
|
"G_BR = MatrixSymbol('G_BR', 3, 4)\n",
|
|
|
|
"G_BR = MatrixSymbol(\"G_BR\", 3, 4)\n",
|
|
|
|
"G_RB = MatrixSymbol('G_RB', 4, 3)\n",
|
|
|
|
"G_RB = MatrixSymbol(\"G_RB\", 4, 3)\n",
|
|
|
|
"G_RR = MatrixSymbol('G_RR', 4, 4)\n",
|
|
|
|
"G_RR = MatrixSymbol(\"G_RR\", 4, 4)\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Create G matrix as a BlockMatrix\n",
|
|
|
|
"# Create G matrix as a BlockMatrix\n",
|
|
|
|
"G = BlockMatrix([\n",
|
|
|
|
"G = BlockMatrix([[G_AA, G_AB, G_AR], [G_BA, G_BB, G_BR], [G_RA, G_RB, G_RR]])\n",
|
|
|
|
" [G_AA, G_AB, G_AR],\n",
|
|
|
|
|
|
|
|
" [G_BA, G_BB, G_BR],\n",
|
|
|
|
|
|
|
|
" [G_RA, G_RB, G_RR]\n",
|
|
|
|
|
|
|
|
"])\n",
|
|
|
|
|
|
|
|
"\n",
|
|
|
|
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Calculate the trace of VA@G@VB@G\n",
|
|
|
|
"# Calculate the trace of VA@G@VB@G\n",
|
|
|
@ -92,47 +91,47 @@
|
|
|
|
],
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"source": [
|
|
|
|
"# Define MatrixSymbols with specific sizes\n",
|
|
|
|
"# Define MatrixSymbols with specific sizes\n",
|
|
|
|
"AA = MatrixSymbol('AA', 2, 2) # 2x2\n",
|
|
|
|
"AA = MatrixSymbol(\"AA\", 2, 2) # 2x2\n",
|
|
|
|
"AB = MatrixSymbol('AB', 2, 3) # 2x3\n",
|
|
|
|
"AB = MatrixSymbol(\"AB\", 2, 3) # 2x3\n",
|
|
|
|
"BA = MatrixSymbol('BA', 3, 2) # 3x2\n",
|
|
|
|
"BA = MatrixSymbol(\"BA\", 3, 2) # 3x2\n",
|
|
|
|
"BB = MatrixSymbol('BB', 3, 3) # 3x3\n",
|
|
|
|
"BB = MatrixSymbol(\"BB\", 3, 3) # 3x3\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Create block matrices VA and VB using BlockMatrix\n",
|
|
|
|
"# Create block matrices VA and VB using BlockMatrix\n",
|
|
|
|
"# R-related terms are replaced with zero matrices\n",
|
|
|
|
"# R-related terms are replaced with zero matrices\n",
|
|
|
|
"VA = BlockMatrix([\n",
|
|
|
|
"VA = BlockMatrix(\n",
|
|
|
|
|
|
|
|
" [\n",
|
|
|
|
" [AA, 0.5 * AB, ZeroMatrix(2, 4)],\n",
|
|
|
|
" [AA, 0.5 * AB, ZeroMatrix(2, 4)],\n",
|
|
|
|
" [0.5 * BA, ZeroMatrix(3, 3), ZeroMatrix(3, 4)],\n",
|
|
|
|
" [0.5 * BA, ZeroMatrix(3, 3), ZeroMatrix(3, 4)],\n",
|
|
|
|
" [ZeroMatrix(4,2), ZeroMatrix(4,3), ZeroMatrix(4,4)]\n",
|
|
|
|
" [ZeroMatrix(4, 2), ZeroMatrix(4, 3), ZeroMatrix(4, 4)],\n",
|
|
|
|
"])\n",
|
|
|
|
" ]\n",
|
|
|
|
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"VB = BlockMatrix([\n",
|
|
|
|
"VB = BlockMatrix(\n",
|
|
|
|
|
|
|
|
" [\n",
|
|
|
|
" [ZeroMatrix(2, 2), 0.5 * AB, ZeroMatrix(2, 4)],\n",
|
|
|
|
" [ZeroMatrix(2, 2), 0.5 * AB, ZeroMatrix(2, 4)],\n",
|
|
|
|
" [0.5 * BA, BB, ZeroMatrix(3, 4)],\n",
|
|
|
|
" [0.5 * BA, BB, ZeroMatrix(3, 4)],\n",
|
|
|
|
" [ZeroMatrix(4,2), ZeroMatrix(4,3), ZeroMatrix(4,4)]\n",
|
|
|
|
" [ZeroMatrix(4, 2), ZeroMatrix(4, 3), ZeroMatrix(4, 4)],\n",
|
|
|
|
"])\n",
|
|
|
|
" ]\n",
|
|
|
|
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Define G matrix symbols with matching dimensions (kept original)\n",
|
|
|
|
"# Define G matrix symbols with matching dimensions (kept original)\n",
|
|
|
|
"G_AA = MatrixSymbol('G_AA', 2, 2)\n",
|
|
|
|
"G_AA = MatrixSymbol(\"G_AA\", 2, 2)\n",
|
|
|
|
"G_AB = MatrixSymbol('G_AB', 2, 3)\n",
|
|
|
|
"G_AB = MatrixSymbol(\"G_AB\", 2, 3)\n",
|
|
|
|
"G_BA = MatrixSymbol('G_BA', 3, 2)\n",
|
|
|
|
"G_BA = MatrixSymbol(\"G_BA\", 3, 2)\n",
|
|
|
|
"G_AR = MatrixSymbol('G_AR', 2, 4)\n",
|
|
|
|
"G_AR = MatrixSymbol(\"G_AR\", 2, 4)\n",
|
|
|
|
"G_RA = MatrixSymbol('G_RA', 4, 2)\n",
|
|
|
|
"G_RA = MatrixSymbol(\"G_RA\", 4, 2)\n",
|
|
|
|
"G_BB = MatrixSymbol('G_BB', 3, 3)\n",
|
|
|
|
"G_BB = MatrixSymbol(\"G_BB\", 3, 3)\n",
|
|
|
|
"G_BR = MatrixSymbol('G_BR', 3, 4)\n",
|
|
|
|
"G_BR = MatrixSymbol(\"G_BR\", 3, 4)\n",
|
|
|
|
"G_RB = MatrixSymbol('G_RB', 4, 3)\n",
|
|
|
|
"G_RB = MatrixSymbol(\"G_RB\", 4, 3)\n",
|
|
|
|
"G_RR = MatrixSymbol('G_RR', 4, 4)\n",
|
|
|
|
"G_RR = MatrixSymbol(\"G_RR\", 4, 4)\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Create G matrix as a BlockMatrix (kept original)\n",
|
|
|
|
"# Create G matrix as a BlockMatrix (kept original)\n",
|
|
|
|
"G = BlockMatrix([\n",
|
|
|
|
"G = BlockMatrix([[G_AA, G_AB, G_AR], [G_BA, G_BB, G_BR], [G_RA, G_RB, G_RR]])\n",
|
|
|
|
" [G_AA, G_AB, G_AR],\n",
|
|
|
|
|
|
|
|
" [G_BA, G_BB, G_BR],\n",
|
|
|
|
|
|
|
|
" [G_RA, G_RB, G_RR]\n",
|
|
|
|
|
|
|
|
"])\n",
|
|
|
|
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Calculate the product and trace\n",
|
|
|
|
"# Calculate the product and trace\n",
|
|
|
|
"product = block_collapse(VA @ G @ VB @ G)\n",
|
|
|
|
"product = block_collapse(VA @ G @ VB @ G)\n",
|
|
|
|
"result = trace(product)\n",
|
|
|
|
"result = trace(product)\n",
|
|
|
|
"print(result)\n"
|
|
|
|
"print(result)"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -152,39 +151,39 @@
|
|
|
|
"from sympy import MatrixSymbol, BlockMatrix, ZeroMatrix, trace, block_collapse\n",
|
|
|
|
"from sympy import MatrixSymbol, BlockMatrix, ZeroMatrix, trace, block_collapse\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Define MatrixSymbols with specific sizes\n",
|
|
|
|
"# Define MatrixSymbols with specific sizes\n",
|
|
|
|
"AA = MatrixSymbol('AA', 2, 2) # 2x2\n",
|
|
|
|
"AA = MatrixSymbol(\"AA\", 2, 2) # 2x2\n",
|
|
|
|
"BB = MatrixSymbol('BB', 3, 3) # 3x3\n",
|
|
|
|
"BB = MatrixSymbol(\"BB\", 3, 3) # 3x3\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Create block matrices VA and VB using BlockMatrix with only AA and BB\n",
|
|
|
|
"# Create block matrices VA and VB using BlockMatrix with only AA and BB\n",
|
|
|
|
"VA = BlockMatrix([\n",
|
|
|
|
"VA = BlockMatrix(\n",
|
|
|
|
|
|
|
|
" [\n",
|
|
|
|
" [AA, ZeroMatrix(2, 3), ZeroMatrix(2, 4)],\n",
|
|
|
|
" [AA, ZeroMatrix(2, 3), ZeroMatrix(2, 4)],\n",
|
|
|
|
" [ZeroMatrix(3, 2), ZeroMatrix(3, 3), ZeroMatrix(3, 4)],\n",
|
|
|
|
" [ZeroMatrix(3, 2), ZeroMatrix(3, 3), ZeroMatrix(3, 4)],\n",
|
|
|
|
" [ZeroMatrix(4,2), ZeroMatrix(4,3), ZeroMatrix(4,4)]\n",
|
|
|
|
" [ZeroMatrix(4, 2), ZeroMatrix(4, 3), ZeroMatrix(4, 4)],\n",
|
|
|
|
"])\n",
|
|
|
|
" ]\n",
|
|
|
|
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"VB = BlockMatrix([\n",
|
|
|
|
"VB = BlockMatrix(\n",
|
|
|
|
|
|
|
|
" [\n",
|
|
|
|
" [ZeroMatrix(2, 2), ZeroMatrix(2, 3), ZeroMatrix(2, 4)],\n",
|
|
|
|
" [ZeroMatrix(2, 2), ZeroMatrix(2, 3), ZeroMatrix(2, 4)],\n",
|
|
|
|
" [ZeroMatrix(3, 2), BB, ZeroMatrix(3, 4)],\n",
|
|
|
|
" [ZeroMatrix(3, 2), BB, ZeroMatrix(3, 4)],\n",
|
|
|
|
" [ZeroMatrix(4,2), ZeroMatrix(4,3), ZeroMatrix(4,4)]\n",
|
|
|
|
" [ZeroMatrix(4, 2), ZeroMatrix(4, 3), ZeroMatrix(4, 4)],\n",
|
|
|
|
"])\n",
|
|
|
|
" ]\n",
|
|
|
|
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Define G matrix symbols with matching dimensions\n",
|
|
|
|
"# Define G matrix symbols with matching dimensions\n",
|
|
|
|
"G_AA = MatrixSymbol('G_AA', 2, 2)\n",
|
|
|
|
"G_AA = MatrixSymbol(\"G_AA\", 2, 2)\n",
|
|
|
|
"G_AB = MatrixSymbol('G_AB', 2, 3)\n",
|
|
|
|
"G_AB = MatrixSymbol(\"G_AB\", 2, 3)\n",
|
|
|
|
"G_BA = MatrixSymbol('G_BA', 3, 2)\n",
|
|
|
|
"G_BA = MatrixSymbol(\"G_BA\", 3, 2)\n",
|
|
|
|
"G_AR = MatrixSymbol('G_AR', 2, 4)\n",
|
|
|
|
"G_AR = MatrixSymbol(\"G_AR\", 2, 4)\n",
|
|
|
|
"G_RA = MatrixSymbol('G_RA', 4, 2)\n",
|
|
|
|
"G_RA = MatrixSymbol(\"G_RA\", 4, 2)\n",
|
|
|
|
"G_BB = MatrixSymbol('G_BB', 3, 3)\n",
|
|
|
|
"G_BB = MatrixSymbol(\"G_BB\", 3, 3)\n",
|
|
|
|
"G_BR = MatrixSymbol('G_BR', 3, 4)\n",
|
|
|
|
"G_BR = MatrixSymbol(\"G_BR\", 3, 4)\n",
|
|
|
|
"G_RB = MatrixSymbol('G_RB', 4, 3)\n",
|
|
|
|
"G_RB = MatrixSymbol(\"G_RB\", 4, 3)\n",
|
|
|
|
"G_RR = MatrixSymbol('G_RR', 4, 4)\n",
|
|
|
|
"G_RR = MatrixSymbol(\"G_RR\", 4, 4)\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Create G matrix as a BlockMatrix\n",
|
|
|
|
"# Create G matrix as a BlockMatrix\n",
|
|
|
|
"G = BlockMatrix([\n",
|
|
|
|
"G = BlockMatrix([[G_AA, G_AB, G_AR], [G_BA, G_BB, G_BR], [G_RA, G_RB, G_RR]])\n",
|
|
|
|
" [G_AA, G_AB, G_AR],\n",
|
|
|
|
|
|
|
|
" [G_BA, G_BB, G_BR],\n",
|
|
|
|
|
|
|
|
" [G_RA, G_RB, G_RR]\n",
|
|
|
|
|
|
|
|
"])\n",
|
|
|
|
|
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"# Calculate the product and simplify\n",
|
|
|
|
"# Calculate the product and simplify\n",
|
|
|
|
"product = block_collapse(VA @ G @ VB @ G)\n",
|
|
|
|
"product = block_collapse(VA @ G @ VB @ G)\n",
|
|
|
|