# Tag Info

47

I feel this is a good opportunity to list some error-checking techniques. I will discuss those I'm aware of, and please feel free to edit this post and add more. I think the main questions to answer here are what we would like a function to return in the case of error, and how to do this technically. What to return on error I can see 3 different ...

27

MatrixForm is a wrapper that pretty-prints your matrices. When you do the following: cov = {{0.02, -0.01}, {-0.01, 0.04}} // MatrixForm you're assigning the prettified matrix to cov (i.e., wrapped inside a MatrixForm). This is not accepted as an input by most functions (perhaps all) that take matrix arguments. What you should be doing to actually assign ...

27

While I wait for better answers from some very knowledgeable people in the matter on the site, I'll write what I'm thinking... I think that most of your problems are due to lack of practice with functional thinking rather than lack of debugability itself. I think one that on the contrary, one of the advantages of programming functionally is that the state ...

25

While I agree that the debugging tools could have been better developed by now, let me just throw in a few notes and links. Function chaining (f[g[h[...]]]): I'd argue that this is a good thing. Why: Functions return expressions, which are immutable. You don't introduce as much state (or at all), as in imperative languages. This makes it easier to debug ...

20

Like in other programming languages, such as C or Java, assertions are used to catch errors in the logic of your code. With discipline, you can also use exceptions for a similar purpose (see e.g. this discussion for an example). Using patterns and returning a function unevaluated is useful in different types of situations. The linked above answer also ...

19

The problem lies in g[x_] := D[f[x], x]; remember that what SetDelayed (that is, :=) does is to replace stuff on the right corresponding to patterns on the left before evaluating. Thus, when one does something like g[2] (and something like this happens within Plot[]), you are in fact evaluating D[f[2], 2], and since one cannot differentiate with respect to a ...

16

Two of the most common error messages that users encounter when working with parts of lists are Part::partd and Part::partw (look up Message for the error message syntax). Both of these are because the user is trying to access an invalid part of the expression (the "object" referred to in the error message), but there's a subtle difference between the two: ...

16

A bit of historic background Before Mathematica version 6 graphics were produced as a side-effect much as Print works now. In fact you can load this old system using: << Version5Graphics Now you get this behavior: Note that the output is - Graphics - and the plot itself is handled like Print. Since there was often little value in having - ...

15

The problem we encounter here is an instance of rather unexpected limitations of equation solving functionality (i.e. Modulus option in Reduce), e.g. this question : Strange behaviour of Reduce for Mod[x,1] provides another example which has been fixed in the newest version (9.0) of Mathematica. Since Modulus unexpectedly doesn't work here we can take ...

15

To access the errors, you need to invoke the Front End directly from the kernel. In effect, you end up telling the kernel to tell the FE to tell the kernel to do something, so that the FE can report any errors it finds. The method I use is SetAttributes[getFrontEndErrors, HoldAll]; getFrontEndErrors[gexpr_] := Module[{nb}, ...

14

You can switch off the 1/0 messages with Off[Power::infy] Now 1/0 only returns ComplexInfinity. If you want to intercept that (your "prevent the divide-by-zero operation from happening in the first place" seems to imply that) you'd have to redefine Power[0,-1]: Unprotect[Power] Power[0, -1] = ...; Protect[Power] with '...' a definition of your choice. ...

13

Here is a function findBadSets that will find any explicitly bad Set/SetDelayed attempts in a given expression. Simply wrap it around a syntactically complete block of code, or follow the block with // findBadSets and the errors are printed one per row, protected symbol followed by complete left-hand side for each bad Set: (* your example *) // findBadSets ...

13

I would use the fact that Mathematica's pattern matching goes from specific to general. Mathematica determines that the new transformation rule is more specific than a rule already present, and would never be used if it were placed after this rule. In this case, the new rule is placed before the old one. Note that in many cases it is not possible to ...

12

J. M. has already given you a good answer to your question on the General::ivar error and how to avoid it. I wanted to demonstrate another example in the same spirit, where the difference between Set and SetDelayed is important, but you don't have the luxury of getting an error message like you did in this case, because it isn't semantically/syntactically ...

12

You could remove the zero from the denominator, and the corresponding entry from the numerator: a = {1, 2, 3, 4}; b = {5, 6, 0, 8}; Pick[a, Positive[b]]/Pick[b, Positive[b]] (* ==> {1/5, 1/3, 1/2} *)

12

This is because the number you used is extremely large. The number of iterations supported (in either Table or Do) seems to be $2^{31}-1$, i.e. the maximum size of a signed machine integer. I believe this is also an upper bound on the size of an array in Mathematica. This limitation is not unreasonable: the size of the Table you are trying to construct is ...

12

I've just uploaded a new version which again should support loading FeynArts, so that FeynCalc and FeynArts can be used from the same session - which I personally find very convenient. See http://www.feyncalc.org/download/ and http://www.feyncalc.org/cgi-bin/diary.pl FeynArts-3.7 is bundled with the release, so you can simply load FeynCalc with ...

12

You can do the analytic integral in Mathematica too, by telling it to perform the upper integration limit as follows: With[ { i = Integrate[((r^3 - 7)^(2/3)*(1 - (r^3 - 7)^(2/3)/r^2))/r^3, r] }, Simplify[ Limit[i, r -> Infinity] - i /. r -> 2 ] ] (* ==> 23/64 + Pi/(3 Sqrt[3]) - 2 (-(1/7))^(1/3) Hypergeometric2F1[1/3, 1/3, 4/3, ...

12

It's not a syntax mistake. Consider it a suggestion, FrontEnd tells you: "maybe you want to plot it as it has no much sense now". Of course it may have sense, it's just a suggestion. If you take a closer look you will find that storing such Plot makes more sense for FE:

11

There is a certain impedance mismatch between lines of code and Mathematica expressions, because Mathematica code is written more or less directly in the parse trees, and the syntax (which encourages nested expressions) was not particularly designed to make lines a really good concept here. That said, this would be a problem in any language, to various ...

11

MatrixForm is a function to prettyprint matrices and cannot be used in computations. Just leave the MatrixForm away and you're fine: a = {{1, 0, 1, 0}, {2, 1, 1, 1}, {1, 2, 1, 0}, {0, 1, 1, 1}}; inv = Inverse[a]; b = {{0}, {0}, {0}, {1}}; soln = inv.b {{-(1/2)}, {0}, {1/2}, {1/2}} If you want that result displayed (!) in a nice readable way, you can ...

11

Here's another, reliable way: messages = {} clearMessages[] := messages = {} collectMessages[m_] := AppendTo[messages, m] InternalAddHandler["Message", collectMessages] Then do clearMessages[] 1/0; 0/0; messages InternalRemoveHandler["Message", collectMessages] Reference and details: How to abort on any message generated?

10

To understand why you're getting that error, try your code with _ as the pattern and see what elements are returned: list[[Sequence @@ #]] & /@ Position[list, _] (* {List, List, 1, "A", {1, "A"}, List, 1, "B", {1, "B"}, List, 2, "C", {2, "C"}, {{1, "A"}, {1, "B"}, {2, "C"}}} *) You can see that in your case, Position is walking down every branch ...

10

I assume this is caused by the dynamic content becoming active and trying to run its internal code involving standardBasis which is at that moment undefined (and you can't do a Parton an undefined variable). You may add SaveDefinitions->True to your manipulate to store definitions it depends on. SaveDefinitions has the disadvantage that it may cause ...

10

You could try something like the following. bounded[Indeterminate] := \$MaxMachineNumber; bounded[x_?NumericQ] := x This gives a very large number instead of Indeterminate so NMinimize keeps searching. NMinimize[bounded[J[{θ0, θ1, θ2}, X1, y1]], {θ0, θ1, θ2}] (* {0.203498, {θ0 -> -25.1613, θ1 -> 0.206231, θ2 -> 0.201471}} *) Incidentally, ...

10

The function Shuffle is not defined. If you define it (say, replace it with RandomSample) it works. Apparently, Rotate in the latter part of the code is being applied to the output of a function that uses edgeNoise which, in turn, (because Shuffle is undefined) is producing the error message you are seeing. To replicate what is happening in a simple setting ...

10

No, Log is the name of the function and Log[x] is the function applied to x. Using Log without the argument is accepted by the system because Log is a symbol just like any other, but it does not make any sense. The correct way to write it is Solve[Log[x]/x^2 == y, x] or Reduce[Log[x]/x^2 == y, x] The latter tries to give you full solution ...

10

David's answer is correct and the one you need to solve your specific problem. I thought nonetheless that it is worth providing some additional information that might help explain how to diagnose similar issues. Matrix/tensor operations like Dot and Inverse are designed to work with lists, that is, expressions with a Head of List. It also works with ...

10

While the question has been more than answered there are still some things that seem to me worth adding. The first is that, in my opinion, MatrixForm is "essentially" obsolete. If you wish your matrices always look like matrices (in the output) you can set the format type of output cells to TraditionalForm (use the Appearance tab in the Preferences menu). In ...

10

Believe the numerical one. Mathematica simply could not do the symbolic integration. Symbolic integration will travel via a different code path. Here the symbolic integration done using Maple, and it agrees with the numerical solution given by Mathematica's NIntegrate The analytical answer is (7/18)*hypergeom([-1/3, 1, 1], [2, 2], ...

Only top voted, non community-wiki answers of a minimum length are eligible