Define different actions for the same operator depending on the nature of its arguments












2












$begingroup$


I am working with terms of the form



A**B1**B2


where A, B1, and B2 are at this point abstract operators and ** means NonCommutativeMultiply.



I'd like to manipulate these expressions at an abstract level, and then only later substitute explicit matrix operations. Specifically, $A$ will be a square matrix, while $B1$ and $B2$ will be commensurate vectors.



The trick is that at this point the specific meanings of the two occurrences of ** will change. Specifically, A ** B1 will become a matrix multiplication, and B1 ** B2 will become an outer product.



I'm using a package (NCAlgebra) to simplify the expressions at an abstract level, and I want the special handling of NonCommutativeMultiply to apply then. So it would be good if there were some way to create an operation that behaves in all respects as NonCommutativeMultiply (so that the package still recognizes it as such), except in a certain function of my design (so that it gets substituted for the correct explicit operation).



Is there a way I can accomplish this?



Edit: A perhaps-important complication is that in the general case, once made explicit the matrix dimensions will be such that the matrix multiplication A ** B1 will not be defined until the outer product B1 ** B2 has been performed.










share|improve this question











$endgroup$












  • $begingroup$
    How are we supposed to distinguish between matrix multiplication and outer product? It looks the same to me.
    $endgroup$
    – Somos
    Mar 14 at 17:47










  • $begingroup$
    Do you mean mathematically or in the code? Mathematically the two vectors are mapped by the outer product to a square matrix. In the code we would need to create a new function for at least one. I would like that function to behave as NonCommutativeMultiply, except when otherwise specified.
    $endgroup$
    – AGML
    Mar 14 at 17:49










  • $begingroup$
    All I see is A**B1**B2 and the two ** look the same to me. How are we supposed to know that they are supposed to be different?
    $endgroup$
    – Somos
    Mar 14 at 17:52












  • $begingroup$
    We of course cannot, at present. To solve the problem one of the two ** needs to be replaced by a new function. I have used ** in both cases because I want the new function to match the same existing patterns as ** would.
    $endgroup$
    – AGML
    Mar 14 at 17:53










  • $begingroup$
    Alternatively, ** could be used in both cases, but the replacement rule could e.g. substitute an outer product only when the inner product is not defined, or something along those lines.
    $endgroup$
    – AGML
    Mar 14 at 17:54
















2












$begingroup$


I am working with terms of the form



A**B1**B2


where A, B1, and B2 are at this point abstract operators and ** means NonCommutativeMultiply.



I'd like to manipulate these expressions at an abstract level, and then only later substitute explicit matrix operations. Specifically, $A$ will be a square matrix, while $B1$ and $B2$ will be commensurate vectors.



The trick is that at this point the specific meanings of the two occurrences of ** will change. Specifically, A ** B1 will become a matrix multiplication, and B1 ** B2 will become an outer product.



I'm using a package (NCAlgebra) to simplify the expressions at an abstract level, and I want the special handling of NonCommutativeMultiply to apply then. So it would be good if there were some way to create an operation that behaves in all respects as NonCommutativeMultiply (so that the package still recognizes it as such), except in a certain function of my design (so that it gets substituted for the correct explicit operation).



Is there a way I can accomplish this?



Edit: A perhaps-important complication is that in the general case, once made explicit the matrix dimensions will be such that the matrix multiplication A ** B1 will not be defined until the outer product B1 ** B2 has been performed.










share|improve this question











$endgroup$












  • $begingroup$
    How are we supposed to distinguish between matrix multiplication and outer product? It looks the same to me.
    $endgroup$
    – Somos
    Mar 14 at 17:47










  • $begingroup$
    Do you mean mathematically or in the code? Mathematically the two vectors are mapped by the outer product to a square matrix. In the code we would need to create a new function for at least one. I would like that function to behave as NonCommutativeMultiply, except when otherwise specified.
    $endgroup$
    – AGML
    Mar 14 at 17:49










  • $begingroup$
    All I see is A**B1**B2 and the two ** look the same to me. How are we supposed to know that they are supposed to be different?
    $endgroup$
    – Somos
    Mar 14 at 17:52












  • $begingroup$
    We of course cannot, at present. To solve the problem one of the two ** needs to be replaced by a new function. I have used ** in both cases because I want the new function to match the same existing patterns as ** would.
    $endgroup$
    – AGML
    Mar 14 at 17:53










  • $begingroup$
    Alternatively, ** could be used in both cases, but the replacement rule could e.g. substitute an outer product only when the inner product is not defined, or something along those lines.
    $endgroup$
    – AGML
    Mar 14 at 17:54














2












2








2





$begingroup$


I am working with terms of the form



A**B1**B2


where A, B1, and B2 are at this point abstract operators and ** means NonCommutativeMultiply.



I'd like to manipulate these expressions at an abstract level, and then only later substitute explicit matrix operations. Specifically, $A$ will be a square matrix, while $B1$ and $B2$ will be commensurate vectors.



The trick is that at this point the specific meanings of the two occurrences of ** will change. Specifically, A ** B1 will become a matrix multiplication, and B1 ** B2 will become an outer product.



I'm using a package (NCAlgebra) to simplify the expressions at an abstract level, and I want the special handling of NonCommutativeMultiply to apply then. So it would be good if there were some way to create an operation that behaves in all respects as NonCommutativeMultiply (so that the package still recognizes it as such), except in a certain function of my design (so that it gets substituted for the correct explicit operation).



Is there a way I can accomplish this?



Edit: A perhaps-important complication is that in the general case, once made explicit the matrix dimensions will be such that the matrix multiplication A ** B1 will not be defined until the outer product B1 ** B2 has been performed.










share|improve this question











$endgroup$




I am working with terms of the form



A**B1**B2


where A, B1, and B2 are at this point abstract operators and ** means NonCommutativeMultiply.



I'd like to manipulate these expressions at an abstract level, and then only later substitute explicit matrix operations. Specifically, $A$ will be a square matrix, while $B1$ and $B2$ will be commensurate vectors.



The trick is that at this point the specific meanings of the two occurrences of ** will change. Specifically, A ** B1 will become a matrix multiplication, and B1 ** B2 will become an outer product.



I'm using a package (NCAlgebra) to simplify the expressions at an abstract level, and I want the special handling of NonCommutativeMultiply to apply then. So it would be good if there were some way to create an operation that behaves in all respects as NonCommutativeMultiply (so that the package still recognizes it as such), except in a certain function of my design (so that it gets substituted for the correct explicit operation).



Is there a way I can accomplish this?



Edit: A perhaps-important complication is that in the general case, once made explicit the matrix dimensions will be such that the matrix multiplication A ** B1 will not be defined until the outer product B1 ** B2 has been performed.







function-construction operators argument-patterns






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 14 at 17:12







AGML

















asked Mar 14 at 17:04









AGMLAGML

1607




1607












  • $begingroup$
    How are we supposed to distinguish between matrix multiplication and outer product? It looks the same to me.
    $endgroup$
    – Somos
    Mar 14 at 17:47










  • $begingroup$
    Do you mean mathematically or in the code? Mathematically the two vectors are mapped by the outer product to a square matrix. In the code we would need to create a new function for at least one. I would like that function to behave as NonCommutativeMultiply, except when otherwise specified.
    $endgroup$
    – AGML
    Mar 14 at 17:49










  • $begingroup$
    All I see is A**B1**B2 and the two ** look the same to me. How are we supposed to know that they are supposed to be different?
    $endgroup$
    – Somos
    Mar 14 at 17:52












  • $begingroup$
    We of course cannot, at present. To solve the problem one of the two ** needs to be replaced by a new function. I have used ** in both cases because I want the new function to match the same existing patterns as ** would.
    $endgroup$
    – AGML
    Mar 14 at 17:53










  • $begingroup$
    Alternatively, ** could be used in both cases, but the replacement rule could e.g. substitute an outer product only when the inner product is not defined, or something along those lines.
    $endgroup$
    – AGML
    Mar 14 at 17:54


















  • $begingroup$
    How are we supposed to distinguish between matrix multiplication and outer product? It looks the same to me.
    $endgroup$
    – Somos
    Mar 14 at 17:47










  • $begingroup$
    Do you mean mathematically or in the code? Mathematically the two vectors are mapped by the outer product to a square matrix. In the code we would need to create a new function for at least one. I would like that function to behave as NonCommutativeMultiply, except when otherwise specified.
    $endgroup$
    – AGML
    Mar 14 at 17:49










  • $begingroup$
    All I see is A**B1**B2 and the two ** look the same to me. How are we supposed to know that they are supposed to be different?
    $endgroup$
    – Somos
    Mar 14 at 17:52












  • $begingroup$
    We of course cannot, at present. To solve the problem one of the two ** needs to be replaced by a new function. I have used ** in both cases because I want the new function to match the same existing patterns as ** would.
    $endgroup$
    – AGML
    Mar 14 at 17:53










  • $begingroup$
    Alternatively, ** could be used in both cases, but the replacement rule could e.g. substitute an outer product only when the inner product is not defined, or something along those lines.
    $endgroup$
    – AGML
    Mar 14 at 17:54
















$begingroup$
How are we supposed to distinguish between matrix multiplication and outer product? It looks the same to me.
$endgroup$
– Somos
Mar 14 at 17:47




$begingroup$
How are we supposed to distinguish between matrix multiplication and outer product? It looks the same to me.
$endgroup$
– Somos
Mar 14 at 17:47












$begingroup$
Do you mean mathematically or in the code? Mathematically the two vectors are mapped by the outer product to a square matrix. In the code we would need to create a new function for at least one. I would like that function to behave as NonCommutativeMultiply, except when otherwise specified.
$endgroup$
– AGML
Mar 14 at 17:49




$begingroup$
Do you mean mathematically or in the code? Mathematically the two vectors are mapped by the outer product to a square matrix. In the code we would need to create a new function for at least one. I would like that function to behave as NonCommutativeMultiply, except when otherwise specified.
$endgroup$
– AGML
Mar 14 at 17:49












$begingroup$
All I see is A**B1**B2 and the two ** look the same to me. How are we supposed to know that they are supposed to be different?
$endgroup$
– Somos
Mar 14 at 17:52






$begingroup$
All I see is A**B1**B2 and the two ** look the same to me. How are we supposed to know that they are supposed to be different?
$endgroup$
– Somos
Mar 14 at 17:52














$begingroup$
We of course cannot, at present. To solve the problem one of the two ** needs to be replaced by a new function. I have used ** in both cases because I want the new function to match the same existing patterns as ** would.
$endgroup$
– AGML
Mar 14 at 17:53




$begingroup$
We of course cannot, at present. To solve the problem one of the two ** needs to be replaced by a new function. I have used ** in both cases because I want the new function to match the same existing patterns as ** would.
$endgroup$
– AGML
Mar 14 at 17:53












$begingroup$
Alternatively, ** could be used in both cases, but the replacement rule could e.g. substitute an outer product only when the inner product is not defined, or something along those lines.
$endgroup$
– AGML
Mar 14 at 17:54




$begingroup$
Alternatively, ** could be used in both cases, but the replacement rule could e.g. substitute an outer product only when the inner product is not defined, or something along those lines.
$endgroup$
– AGML
Mar 14 at 17:54










1 Answer
1






active

oldest

votes


















4












$begingroup$

If I understand your question correctly, then the ** can be given a definition to do what you want. This code may work for you



Unprotect[NonCommutativeMultiply];
ClearAll[NonCommutativeMultiply];
NonCommutativeMultiply[x_?NumericQ, y_?NumericQ] := x * y;
NonCommutativeMultiply[x_?MatrixQ, y_?MatrixQ] := x . y;
NonCommutativeMultiply[x_?ListQ, y_?ListQ] := Outer[Times, x, y];


You can add more checking in the code as you need to.






share|improve this answer









$endgroup$













  • $begingroup$
    Yes, that should work I think. Thank you!
    $endgroup$
    – AGML
    Mar 14 at 18:17











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f193255%2fdefine-different-actions-for-the-same-operator-depending-on-the-nature-of-its-ar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4












$begingroup$

If I understand your question correctly, then the ** can be given a definition to do what you want. This code may work for you



Unprotect[NonCommutativeMultiply];
ClearAll[NonCommutativeMultiply];
NonCommutativeMultiply[x_?NumericQ, y_?NumericQ] := x * y;
NonCommutativeMultiply[x_?MatrixQ, y_?MatrixQ] := x . y;
NonCommutativeMultiply[x_?ListQ, y_?ListQ] := Outer[Times, x, y];


You can add more checking in the code as you need to.






share|improve this answer









$endgroup$













  • $begingroup$
    Yes, that should work I think. Thank you!
    $endgroup$
    – AGML
    Mar 14 at 18:17
















4












$begingroup$

If I understand your question correctly, then the ** can be given a definition to do what you want. This code may work for you



Unprotect[NonCommutativeMultiply];
ClearAll[NonCommutativeMultiply];
NonCommutativeMultiply[x_?NumericQ, y_?NumericQ] := x * y;
NonCommutativeMultiply[x_?MatrixQ, y_?MatrixQ] := x . y;
NonCommutativeMultiply[x_?ListQ, y_?ListQ] := Outer[Times, x, y];


You can add more checking in the code as you need to.






share|improve this answer









$endgroup$













  • $begingroup$
    Yes, that should work I think. Thank you!
    $endgroup$
    – AGML
    Mar 14 at 18:17














4












4








4





$begingroup$

If I understand your question correctly, then the ** can be given a definition to do what you want. This code may work for you



Unprotect[NonCommutativeMultiply];
ClearAll[NonCommutativeMultiply];
NonCommutativeMultiply[x_?NumericQ, y_?NumericQ] := x * y;
NonCommutativeMultiply[x_?MatrixQ, y_?MatrixQ] := x . y;
NonCommutativeMultiply[x_?ListQ, y_?ListQ] := Outer[Times, x, y];


You can add more checking in the code as you need to.






share|improve this answer









$endgroup$



If I understand your question correctly, then the ** can be given a definition to do what you want. This code may work for you



Unprotect[NonCommutativeMultiply];
ClearAll[NonCommutativeMultiply];
NonCommutativeMultiply[x_?NumericQ, y_?NumericQ] := x * y;
NonCommutativeMultiply[x_?MatrixQ, y_?MatrixQ] := x . y;
NonCommutativeMultiply[x_?ListQ, y_?ListQ] := Outer[Times, x, y];


You can add more checking in the code as you need to.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 14 at 18:10









SomosSomos

1,635110




1,635110












  • $begingroup$
    Yes, that should work I think. Thank you!
    $endgroup$
    – AGML
    Mar 14 at 18:17


















  • $begingroup$
    Yes, that should work I think. Thank you!
    $endgroup$
    – AGML
    Mar 14 at 18:17
















$begingroup$
Yes, that should work I think. Thank you!
$endgroup$
– AGML
Mar 14 at 18:17




$begingroup$
Yes, that should work I think. Thank you!
$endgroup$
– AGML
Mar 14 at 18:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f193255%2fdefine-different-actions-for-the-same-operator-depending-on-the-nature-of-its-ar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?