在 Mathematica 中以明确可见的形式显示

发布于 2024-10-21 00:48:07 字数 341 浏览 3 评论 0原文

继续我的矩阵乘法问题 ,我想在 mma 中以显式可视形式显示以下表达式:

我想在 mma 中显示的形式

即使在在我给出 a11, ..., b11, ... 显式数字的情况下,我仍然希望它是未评估形式的 (0&&1)||(1&1) 。有人可以帮忙吗?

Continuing with my matrix multiplication question, I want to show the following expression in explicit viewable form in mma:

the form I want to show in mma

Even if in the case I give a11, ..., b11, ... explicit numbers, I still want it to be (0&&1)||(1&&1) in unevaluated form. Can anyone please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缺⑴份安定 2024-10-28 00:48:08

(0&&1)||(1&&1) 不评估,所以我没有看到问题。对于 TrueFalse 您是否尝试过使用 HoldForm?

(0&&1)||(1&&1) does not evaluate, so I do not see the problem. For True and False have you tried using HoldForm?

圈圈圆圆圈圈 2024-10-28 00:48:08

实现此目的的一种方法是定义您自己的矩阵包装器。包装器方法的优点是您可以重载任意数量的内置函数,而不会影响任何其他功能。

让我们首先定义一个名为 myMatrix 的包装器,它使用 MatrixForm 显示自身:

Format[myMatrix[m_]] ^:= MatrixForm[m]

接下来,我们将在 Times 运算符作用于 时重载它。 >myMatrix

myMatrix[m1_] myMatrix[m2_] ^:= myMatrix[Inner[And, m1, m2, Or]]

请注意,这两个定义都使用 ^:= 将规则作为上值附加到 myMatrix。这对于确保常规内置定义不受影响至关重要。

有了这些定义,我们现在就可以实现预期的目标了。为了进行演示,我们定义两个矩阵:

m1 = myMatrix[Array[Subscript[a, Row[{##}]]&, {2, 2}]]

mathematica output

m2 = myMatrix[Array[Subscript[b, Row[{##}]]&, {2, 2}]]

mathematica output

现在可以生成所请求的“显式可见表单”:

Row[{m1, m2}] == m1 m2

mathematica output

。 .. 或者,如果您希望在等式左侧显式引用 Times

With[{m1 = m1, m2 = m2}, HoldForm[m1 m2] == m1 m2]

 mathematica output

接下来,我们将为每个矩阵元素分配随机布尔值:

Evaluate[First /@ {m1, m2}] = RandomInteger[1, {2, 2, 2}];

...然后使用适当的分配再次生成显式可见的形式:

With[{m1 = m1, m2 = m2}, HoldForm[m1 m2] == m1 m2]

mathematica 输出

One way to achieve this is to define your own matrix wrapper. The wrapper approach has the advantage that you can overload as many built-in functions as you like without impacting any other functionality.

Let's start by defining a wrapper called myMatrix that displays itself using MatrixForm:

Format[myMatrix[m_]] ^:= MatrixForm[m]

Next, we'll overload the Times operator when it acts on myMatrix:

myMatrix[m1_] myMatrix[m2_] ^:= myMatrix[Inner[And, m1, m2, Or]]

Note that both definitions use ^:= to attach the rules to myMatrix as up-values. This is crucial to ensure that the regular built-in definitions are otherwise unaffected.

Armed with these definitions, we can now achieve the desired goal. To demonstrate, let's define two matrices:

m1 = myMatrix[Array[Subscript[a, Row[{##}]]&, {2, 2}]]

mathematica output

m2 = myMatrix[Array[Subscript[b, Row[{##}]]&, {2, 2}]]

mathematica output

The requested "explicitly viewable form" can now be generated thus:

Row[{m1, m2}] == m1 m2

mathematica output

... or, if you prefer to reference Times explicitly on the left-hand side of the equation:

With[{m1 = m1, m2 = m2}, HoldForm[m1 m2] == m1 m2]

mathematica output

Next, we'll assign random boolean values to each of the matrix elements:

Evaluate[First /@ {m1, m2}] = RandomInteger[1, {2, 2, 2}];

... and then generate the explicitly viewable form once again with the assignments in place:

With[{m1 = m1, m2 = m2}, HoldForm[m1 m2] == m1 m2]

mathematica output

梦行七里 2024-10-28 00:48:07

使用

Inner[And, Array[Subscript[a, ##] &, {2, 2}], 
  Array[Subscript[b, ##] &, {2, 2}], Or] // MatrixForm

在此处输入图像描述

编辑。跟进您之前的问题后,我认为您可能会考虑

Inner[HoldForm[And[##]] &, amat, 
  bmat, HoldForm[Or[##]] &] // MatrixForm

在此处输入图像描述

Use

Inner[And, Array[Subscript[a, ##] &, {2, 2}], 
  Array[Subscript[b, ##] &, {2, 2}], Or] // MatrixForm

enter image description here

Edit. Having followed up on your previous question, I think you might consider

Inner[HoldForm[And[##]] &, amat, 
  bmat, HoldForm[Or[##]] &] // MatrixForm

enter image description here

百合的盛世恋 2024-10-28 00:48:07

我不认为这是一个真正的好主意(重载内部函数和所有;并且 && 不是 BitAnd,您想在上一个问题中使用它),但是您要求它并得到它:

CircleTimes[a_?MatrixQ, b_?MatrixQ] := 
 Inner[HoldForm[BitAnd[##]] &, a, b, HoldForm[BitOr[##]] &]

Unprotect[BitAnd];
Unprotect[BitOr];
BitAnd /: Format[BitAnd[a_, b_]] := a && b;
BitOr /: Format[BitOr[a_, b_]] := a || b;
Protect[BitAnd];
Protect[BitOr]

mat1 = Array[Subscript[a, #1, #2] &, {2, 2}];
mat2 = Array[Subscript[b, #1, #2] &, {2, 2}];

< img src="https://i.sstatic.net/sK9tA.png" alt="新定义的 CircleTimes 的输出">

将操作定义为 CircleTimes 的优点是您可以免费获得 CircleTimes 符号和运算符。

I don't think this is a really good idea (overloading internal functions and all; and && is And not BitAnd, which you wanted to use in the previous question), but you asked for it and you get it:

CircleTimes[a_?MatrixQ, b_?MatrixQ] := 
 Inner[HoldForm[BitAnd[##]] &, a, b, HoldForm[BitOr[##]] &]

Unprotect[BitAnd];
Unprotect[BitOr];
BitAnd /: Format[BitAnd[a_, b_]] := a && b;
BitOr /: Format[BitOr[a_, b_]] := a || b;
Protect[BitAnd];
Protect[BitOr]

mat1 = Array[Subscript[a, #1, #2] &, {2, 2}];
mat2 = Array[Subscript[b, #1, #2] &, {2, 2}];

output of newly defined CircleTimes

The advantage of defining the operation as CircleTimes is that you get the CircleTimes symbol and operator for free.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文