Skip to main content
30 votes
Accepted

NumericArray versus PackedArray in MMA12

NumericArrays are unrelated to packed arrays. "Packed array" refers to an internal efficient storage format used for Lists. ...
Szabolcs's user avatar
  • 235k
23 votes

Why no RationalQ or RealQ?

There is a RealQ, see Developer`RealQ Also relevant: Developer`MachineRealQ The ...
Henrik Schumacher's user avatar
17 votes

Why no RationalQ or RealQ?

Why no RationalQ or RealQ? Probably because it isn't unambiguous what such a function should do. From the comments above: If there were a RationalQ, I'd expect ...
Jason B.'s user avatar
  • 68.7k
16 votes
Accepted

How to get the type of an object

Example: list = {1, 2, 3}; integer = 1; real = 0.1; Head @ list Head @ integer Head @ real Output: List Integer Real ...
e.doroskevic's user avatar
  • 5,969
15 votes
Accepted

Is it possible to set multiple type to a variable?

You may specify multiple alternative patterns (originally mentioned in a comment): f[x:(_Real|_Integer)] := 2 x; You may specify multiple alternative matches ...
eyorble's user avatar
  • 9,468
14 votes

Using DataStructures in FunctionCompile

This is a bit too long for a comment, but below is a slightly modified version that uses the "DynamicArray" approach. The basic issue is that all the ...
ilian's user avatar
  • 25.5k
12 votes
Accepted

Is it possible to restrict the arguments for a function to a specific entity type?

As pointed out by Kuba in comments, when we make a function definition the pattern variables are not restricted to be simple x_head. So you can create a definition ...
Jason B.'s user avatar
  • 68.7k
11 votes

What is an example of of idiomatic "typed" WolframScript?

Here is an idiom used by many built-in functions to achieve this effect: ...
Jason B.'s user avatar
  • 68.7k
11 votes
Accepted

Make symbols atomic, without losing their type

You could use my TensorSimplify package help with this. Install the paclet with: ...
Carl Woll's user avatar
  • 131k
11 votes

Make symbols atomic, without losing their type

Perhaps what you want is symbolic tensors: http://reference.wolfram.com/language/tutorial/SymbolicTensors.html ...
Szabolcs's user avatar
  • 235k
9 votes
Accepted

Provide definition to FunctionCompile

[This is a full rewrite of my original answer] J. Antonio's question points out how FunctionCompile is indeed still in very early experimental stages of development, that something so simple as ...
Bert Sierra's user avatar
9 votes

How to get the type of an object

(Post adapted after comments.) Mathematica 10 introduced a new type system local to Dataset, that's used like this: ...
C. E.'s user avatar
  • 70.7k
8 votes
Accepted

Immutability, Association and typing

My understanding is that you want to wrap an association into an inert wrapper representing the type. Immutable version Then, you should do just that: ...
Leonid Shifrin's user avatar
7 votes
Accepted

How to use patterns to specify variable type as Boolean (True, False) in analogy to x_Integer, x_Real, x_Complex, x_List, etc

Should be doable with f[y : True | False] := ... Beware that the pattern x_Real checks only whether the ...
Henrik Schumacher's user avatar
5 votes

How to check if an expression is a real-valued number

Since V 13.3 we have RealValuedNumberQ and RealValuedNumericQ ...
eldo's user avatar
  • 75k
5 votes
Accepted

Using DataStructures in FunctionCompile

I give a simplified version below that is slightly faster than the solution from illan. ...
Ted Ersek's user avatar
  • 929
4 votes

FunctionCompiling lists of things

You are correct this type of polymorphism does not yet exist. At compile time the new compiler must be able to determine all of the types. The second type of polymorphism can be used to call ...
A.Richards's user avatar
3 votes

Type formation rules for 1-position expressions

I don't think your table is necessarily incorrect, but I don't think it's the best way to think about it. In Mathematica, everything is an expression (see the linked tutorial for more details). Every1 ...
Lukas Lang's user avatar
  • 34.1k
3 votes
Accepted

How to enforce/specify the type of a module local variables similar to the module arguments?

The code below might work. As an example of the usage of the function type below, for a variable s, type[s,Real]; s=4 sends an error message. First we define the ...
userrandrand's user avatar
  • 5,912
3 votes
Accepted

How to force Mathematica to return `NumericQ` as True when aplied to some variable in Mathematica?

$Version (* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *) Clear["Global`*"] Use TagSet or <...
Bob Hanlon's user avatar
  • 159k
3 votes
Accepted

Is it possible to improve efficiency of the function?

If you could easily enumerate what counts as a letter (e.g. if you can restrict legal letters to the English alphabet), then you can get performance improvement by doing some special case definitions. ...
lericr's user avatar
  • 29.2k
2 votes

Behavior of 1.2 ∈ Rationals changes in v9?

I confirm that versions 5.2 and 8.0.4 return False for 1.2 ∈ Rationals: ... while the latest available versions don't: ...
Alexey Popkov's user avatar
2 votes
Accepted

Specify Variable Size

Don't know how close this is to what you are after: ...
Kuba's user avatar
  • 137k
2 votes

Data Type that can contain multiple data types

Mathematica doesn't need such a thing. "Everything is an expression." In many cases, subexpressions may be of a variety of types. Consider List. Any element of a ...
John Doty's user avatar
  • 13.7k
2 votes

Adding a curve on DensityPlot: Epilog -> First[ContourPlot[...]] why the first?

If you look at the documentation for Graphics, you'll note that there is a list of all allowed primitives given, and that those primitives go into the first ...
Lukas Lang's user avatar
  • 34.1k
2 votes
Accepted

How do I use a FunctionCompile(d) function in FindMInimum

You had the right idea, but _NumericArray will only match explicit NumericArray expressions, not arrays of numbers in general. ...
Simon Woods's user avatar
1 vote

Type Confusion in v13.1.0

After a couple of trials, the difference is how AbsoluteTime interprets a DateObject containing machine-precision numbers. On my ...
MarcoB's user avatar
  • 67.3k
1 vote

Is it possible to restrict the arguments for a function to a specific entity type?

There is EntityTypeName (from 2014) so one could do : g[x_] /; EntityTypeName[x] === "Planet" := x["Mass"] ...
userrandrand's user avatar
  • 5,912
1 vote
Accepted

Who knows what happened in this code? (The difference between 3 and 3.)

I'm not sure that an association is the ideal way to handle this, as the keys must be equivalent in order to get a match. This means that if you use machine precision numbers (...
MassDefect's user avatar
  • 10.1k
1 vote

How to create an unevaluated command like Sequence

Use a HoldAll instead of an unevaluated function, or define your AdjMtx function as HoldAll ...
Sarah Stanley's user avatar

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