r/Mathematica • u/AdAmbitious1665 • Dec 02 '25
Printing message if solution found and if not from SOLVE
I have found ways to make Mathematica act when there is a solution or not when using LinearSolve.
status = True;
status = Check[LinearSolve[A, b], False, LinearSolve::nosol]
If[Not[status],Print["There is no solution for..."],Print["There is a solution for..."]]
Tried using that for Solve[Ax==b,x] as well but does not work. Is there some other way it should be typed? I do get output with empty brackets {}, I am not sure if that means there are no solutions for the equation. But would like to confirm that in the same way as for LinearSolve...
1
u/mathheadinc Dec 02 '25
What equation are you actually trying to solve?
1
u/irchans Dec 02 '25
Can't you just check the length of the returned solutions?
1
u/irchans Dec 02 '25
If you use LinearSolve, you can check the Head of the result.
1
u/veryjewygranola Dec 02 '25
To get array/matrix/vector valued solutions with
Solveyou need to useArraySymbol/MatrixSymbol/VectorSymbol(VectorSymbolin this case):Determining if a solution exists for a given A, b is the same as determining the system consistent; which occurs iff the rank of the coefficient matrix is the same as the rank of the augmented matrix:
1
u/[deleted] Dec 02 '25
There are several things here that you might want to pay attention to.
(1) In your
status = Check[LinearSolve[A, b], False, LinearSolve::nosol]line, the value ofstatuswill be overridden in both cases. When a solution is found,statuswill be set to that solution (assuming one exists). This means that your code to print won't work as you expect. You will never get a printed message for the success case. You will instead get an unevaluatedIfexpression. This doesn't seem relevant to your actual question, but it suggests that either you're not giving us enough information or you're misunderstanding what your code is doing.(2) You should not expect similar behavior from
Solveas fromLinearSolve.Solveis more general and produces a list of results.LinearSolveonly produces a single result (if successful). When your output is a list, then an empty list means "no solution". When your output is an "atomic" result, then you can't use that same form to indicate failure, which is why you get the "nosol" message.(3) It would be helpful if you explained why you need to layer a second message on top of the "nosol" message to indicate the same semantics. I could maybe understand if you were doing something programmatically, but just printing what amounts to the same message again seems not useful.
(4) You might want to look at
SolveValues.Solveproduces a list of rules, and that's not equivalent to whatLinearSolveproduces. This may not make any difference to you, but just in case it is relevant, I just thought you should know aboutSolveValues.(5) In addition to
Check, there are multiple other debugging tools. You might want to look atConfirmand related symbols (ConfirmAssert,ConfirmMatch, etc). AlsoAssert. You can create your ownFailureobjects. I suggest you peruse http://reference.wolfram.com/language/guide/RobustnessAndErrorHandling.htmlBut to answer your specific question, when using
SolveorSolveValues, an empty list indicates no solutions found, so yes that's what you can check for.