# Tag Info

11

Maybe upperTriangularMatrixQ2[mat_?MatrixQ] /; Equal @@ Dimensions@mat := UpperTriangularize@mat == mat; test = RandomInteger[{1, 100}, {1000, 1000}]; upperTriangularMatrixQ@test // AbsoluteTiming {2.126050, False} upperTriangularMatrixQ2@test // AbsoluteTiming {0.003277, False} test2 = UpperTriangularize@test; upperTriangularMatrixQ@test2 ...

9

You did not specify if this test should be optimized for the positive or negative case. If most of your matrices will fail the test it can be greatly beneficial to have an early exit behavior. For example if the lower left element in the matrix is not zero you can fail the matrix after a single element test! And even in the positive case the elements on ...

5

This function yields True if the expression expr is valid, otherwise it yields False: validExpressionQ[ expr_, allowedList_List] := Union @ Cases[ Variables[expr], f_[x___] :> x, Infinity] == Union @ allowedList now we have, e.g. validExpressionQ[ x f[a, b, d] - 4 f[b, a, d] + z f[] + f[a, b, d] - f[a] f[b, d], {a, b, d}] True ...

5

Assuming[n ∈ Integers && n > 0, PolynomialQ[x^n, x]] won't work because Assuming only works on functions with Assumptions option, such as Simplify, Refine, etc. Unfortunately PolynomialQ doesn't have this option. Still, something like Simplify[PolynomialQ[x^n, x], n ∈ Integers && n > 0] won't work because Mathematica will calculate ...

4

Apparently you know somehow that n is a non-negative integer, but Mathematica has no idea. n could be anything. You need to let Mathematica know. Since PolynomialQ doesn't honor Assuming, you would need to make your own that does. Using Simplify could incorporate the assumptions. Here is my attempt at it (though my pattern-fu is not very strong -- e.g. ...

3

I just checked the documentation in V10 and accidentally stumbled upon a built in command: SQLConnectionOpenQ[conn] This seems to do the trick.

3

My apologies to those who closed this question for my unilaterally reopening it, but there is a nontrivial aspect to this question that I wish to address, and it would not nicely fit in comments. (I am not making an exception for myself; when someone has such an answer he wishs to give to a closed question I nearly always reopen it for him to do so.) ...

3

Jump straight down to Update 2 for the final code. I'll leave the previous iterations here as they explain how that solution developed. This is based on the following definition of similarity: Two expressions are similar if they become identical when all variables are replaced by the same generic variable. For example, both $a+b$ and $b+c$ become ...

2

As far as I can tell, in general there is no way to do what you want. One can't generally measure the memory requirements etc. for delayed definitions, because for them the creation of corresponding values happens only at run-time. OTOH, you can't really exclude the delayed definitions, particularly when dealing with the code of others. It is a trade-off. ...

2

A possibility is to define a support function: EDIT: Thanks to Simon Woods' comment: .. instead of __ check[(a | b | d) ..] := 1; check[___] = Indeterminate; The substitution expr /. f-> check contains Indeterminate if f has arguments not allowed. Concluding: If[FreeQ[expr /. f -> check, Indeterminate], "ok", "error"] returns "error" for ...

1

Another improved version TestConstantValuedExpression[expression_,zerovaluetest_]:=Module[{randomvaluestestresult,expressionrandomvalue,previousexpressionrandomvalue,symbolreplacementlist,extremesdifferencetestvalue}, randomvaluestestresult=True; previousexpressionrandomvalue=False; Quiet[ Do[ Block[{\$MaxExtraPrecision=Infinity}, ...

1

Is this what you want? ClearAll@ExactVarsQ; ExactVarsQ[func_, vars__] := MatchQ[ Sort@DeleteDuplicates@Cases[func, x_ /; (AtomQ[x] && ! NumericQ[x]), {0, Infinity}], Sort@DeleteDuplicates@{vars} ] test1 = (1 - Exp[I*(x - 5.6)*23.4])*Log[Abs[x - 4.3]]/(1.7 + 3.2*I - x^2) test2 = (1 - Exp[I*(x - y)*23.4])*Log[Abs[x - 4.3]]/(1.7 + 3.2*I - ...

1

data = {I (a^2 + b^2) (*1*), a (I b + e) (*2*), b (I a + d) (*3*), b (I a + f) (*4*), a b (*5*), a (I b + f) (*6*), I (b^2 + c^2) (*7*)} systemnames = Names["System`*"]; test[expr_] := Select[{Extract[expr, #], #} & /@ Position[expr, _Symbol, Infinity], MemberQ[systemnames , ToString@(#[[1]])] &] Gather[data, test[#1] == test[#2] ...

1

Since it seems that evaluation of the expression is specifically permitted let's leverage that: check[expr_, f_, pat_] := FreeQ[expr /. f[pat ...] -> 1, _f] check[expr, f, a | b | d] check[expr, f, a | b | c | d] False True I used Alternatives instead of List to make this cleaner, but you can always Apply Alternatives if needed. If for some ...

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