3

I am writing a program in which I am using NumericQ. I am trying to allow symbols to also be considered Numeric, so I have been using NumericQ[a]=True for all of the symbols that I want to be numeric. However, this messes with the functionality of Solve so that I have to write NumericQ[a]=False for all of the symbols before using Solve. Going back and forth repeatedly, even using a map function, is annoying. To solve the issue, I created a new function defined by numeric[x_Symbol]=True; numeric[x_?NumericQ]=True; numeric[___]=False;. The issue with this method is that I want numeric[b+5] to evaluate as True. For example, when you type NumericQ[c]=True; NumericQ[c + 7]; evaluates to True. Initially, I added a line for every operation between symbols that I wanted to be True --Plus[x_?numeric,y_?numeric]=True. Even just adding Plus made the code significantly slower, but adding it for all of the operations - Times, Conjugate, Exponent - made it too slow to evaluate. My code makes extensive use of NumericQ, so I need whatever I use to not be much slower than it if I want it to run in a reasonable time. Thank you for the help.

Edit: The reason I want to do this is I am trying to add commutative properties like distributive to NonCommutativeMultiply. The NonCommutativeAlgebra package does this, but the symbols that I want to be noncommutative (one's with a subscript) are commutative in this package. Combining NumericQ with assigning certain symbols as numeric implements what I am wanting to do perfectly, but I have to change those symbols back to not numeric if I want to use the solve function.

  • 2
    If every symbol is numeric, and any composition of numeric things is also numeric, what exactly is not numeric? Can you give an example of things you don't want to consider to be numeric? Also, can you be a bit more specific as to what you need all those checks for? – Lukas Lang Jul 01 '21 at 19:46
  • Can you clarify how it interferes with Solve? I hope you are not doing NumericQ[a] = True then try to Solve for a, as that makes no sense. If you are doing that, then why do you want NumericQ[a] to be True? It only really makes sense to set NumericQ[a] to True if you also set N[a], so a truly behaves like a numeric constant. – Szabolcs Jul 01 '21 at 19:52
  • 1
    Why not use a[1] and give a the NumericFunction attribute? – Carl Woll Jul 01 '21 at 19:53
  • 6
    Overall, this feels like an XY problem: https://xyproblem.info/ More clarifications are needed. I suggest you explain the actual task you want to complete instead of asking about how to implement a solution which is very likely not the way to go. – Szabolcs Jul 01 '21 at 19:53
  • Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory [tour] now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Jul 01 '21 at 20:05
  • Sorry for not being more clear. This is my first time using Mathematica, so I am not sure what all of the best practices are, I have just been focused on writing something that works. I am trying to write a program similar to NCAlgebra that gives NonCommutativeMultiply certain properties like the distributive property and bringing commutative elements (numbers and symbols) to the front. @LukasLang Elements that are not commutative have a subscript. For example, Subscript[S,1] or Subscript[U,2]. (The NCAlgebra package treats these as commutative.) – Davis Ellis Jul 01 '21 at 20:22
  • @Szabolcs I am getting the program to work right now by doing what you are saying, setting NumericQ[a] = True then later when I want to solve for a, I set NumericQ[a] = False. I am using NumericQ as a shortcut to check for elements that I want to commute with NonCommutativeMultiply because of how quickly it runs even if I add symbols that should also be commutative. – Davis Ellis Jul 01 '21 at 20:28
  • Does the noncommutative algebra that you're interested in only have a finite (or clearly defined) list of noncommuting elements? Then you can define CentralQ as in my answer https://mathematica.stackexchange.com/a/165511/29926 , which allows for commuting quantities including symbolic functions and so on – Jules Lamers Jul 02 '21 at 00:57
  • @LukasLang Technically speaking infinities are not numeric... :) – kirma Nov 07 '22 at 10:24

2 Answers2

0

In NCAlgebra you can use subscripts with noncommutative symbols.

SNC[X]
a ** Subscript[X, 1] ** b

evaluates to

a ** Subscript[X, 1] ** b

as you would expect. Subscripted symbols inherit the noncommutative property from their base symbol.

Mauricio de Oliveira
  • 2,001
  • 13
  • 15
0

Rather than adding definitions to Plus, Times etc, which slows everything down tremendously, it should be more efficient to keep the logic in the definition of your numeric test, which I named myNumericQ here. I also include the last line to show how it can be used to make some symbols "numeric".

ClearAll[myNumericQ]
myNumericQ[expr_?NumericQ] := True;
myNumericQ[(Plus | Times | Power |
      head_ /; MemberQ[Attributes[head], NumericFunction])[args___]] := 
  And @@ myNumericQ /@ {args};
myNumericQ[arg_] = False;
myNumericQ[n] = True;
Bruno Le Floch
  • 1,959
  • 10
  • 23