User Tools

Site Tools


numbers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
numbers [2018/10/26 04:14]
julia [Iterating Over All Combinations / Permutations / etc.]
numbers [2018/12/27 17:05] (current)
Line 11: Line 11:
 * [[dicts|dictionaries]]. * [[dicts|dictionaries]].
  
-The [[commandflow|Command ​Flow Chapter]] has already covered [[commandflow#​booleans_and_conditionals|Booleans]].+The [[controlflow|Control ​Flow chapter]] has already covered [[controlflow#​booleans_and_conditionals|Booleans]].
  
  
 ```juliarepl ```juliarepl
-julia> pkgchk( [  "​julia"​ => v"1.0.1", "​Missings"​ => v"​0.3.1",​ "​Formatting"​ => v"​0.3.4",​ "​CategoricalArrays"​ => v"​0.4.0"​ ] )+julia> pkgchk.( [  "​julia"​ => v"1.0.3", "​Missings"​ => v"​0.3.1",​ "​Formatting"​ => v"​0.3.4",​ "​CategoricalArrays"​ => v"​0.4.0"​ ] );
  
 ``` ```
Line 36: Line 36:
 #### USE CONST LIBERALLY #### USE CONST LIBERALLY
  
-FIXME (Andreas:See previous comment about `const` ​and globals+Use '​const' ​(the constant modifiergenerously, especially for global variables that you want to (ab-)use in function. ​ This improves both bug-detection ​and execution speed, often dramatically. ​ Or avoid globals ​altogether.
  
-Use '​const'​ (the constant modifier) generously, especially for global variables. ​ This improves both bug-detection and execution speed, often dramatically. 
  
 +#### DECLARE YOUR FUNCTION SIGNATURES (VARIABLES) AS NARROWLY TYPED AS POSSIBLE
  
-#### DECLARE YOUR VARIABLES AS NARROWLY TYPED AS POSSIBLE+IMHO, primitive native-CPU type objects are best.  This improves bug-detection,​ memory footprint, and speed.
  
-FIXME (Andreas:) This advice is misleading. First of allit's unclear if we are talking about variable declaration or function argument signatures. Stricter signatures might be more informative about the function ​but it won't make the function faster per se. The method will be specialized for the specific input types regardless of signature. An important counter example ​to your statement that broader ​types are overkill in scientific computations is automatic differentiation which is a very powerful computational method. It should be used much more widely than it currently isThe main requirement is that you can pass special number types through and restricting to e.g. `Float64` is therefore typically not a good idea. Annotating ​the variables also won't give any speed up. It might reveal a type instability in a function ​but we usually recommend *not* to annotate variables with types.+Use broader types only when there is a clear need (such as in [[fundispatchs|function arguments]]in which you *really* want the function to have broader ​and more generic reusable functionality) ​This ​can be the case, e.g., if you want to feed the function to an automatic differentiator (see Andreas'​ note below.)
  
-Primitive native-CPU type objects ​are best.  ​This improves bug-detectionmemory footprint, and execution speedoften dramatically.+More generic numeric types---such as `Real` (which can be either `Int*` or `Float*`); and `Number` (which can be either `Real` or `Complex`)---are almost always useless overkill in specific scientific application programming.  ​The `Any` type is even broaderso avoid it like the undead dead. (The exception to the 'do not use Any' rule is printingwhere `Any` printing can indeed be quite useful.)
  
-Use broader types only when there is a clear need (such as in [[fundispatchs|function ​arguments]],​ in which you *reallywant the function ​to have broader and more generic reusable functionality).+Unfortunately,​ global variables cannot have types in Julia 1.0 yet. 
 + 
 + 
 +My advice here is controversial. ​  ​Andreas Noack, for example, points out that stricter signatures might be more informative about the function but they won't make the function faster per se.  The method will become specialized for the specific input types regardless of signature. ​ An important counter example to your [preference for broader types] in scientific computations is automatic differentiation. ​ This is a very powerful computational method. ​ It should be used much more widely than it currently is. The main requirement is that one can pass special number types through and restricting to e.g. `Float64` is therefore typically not a good idea. Annotating the variables also will not give any speed up.  It might reveal a type instability ​in function ​but we usually recommend ​*not* to annotate variables with types.
  
-More generic numeric types---such as `Real` (which can be either `Int*` or `Float*`); and `Number` (which can be either `Real` or `Complex`)---are almost always useless overkill in specific scientific application programming. ​ The `Any` type is even broader, so avoid it like the undead dead. (The exception to the 'do not use Any' rule is printing, where `Any` printing can indeed be quite useful.) 
  
-Unfortunately,​ global variables cannot have types in Julia 0.6.2. ​ This will be fixed in the future. 
  
  
Line 210: Line 211:
 true true
  
-julia> x= 3; y= 3;  (x === y) ## dereferenced variables'​ contents are type and value identical+julia> x= 3; y= 3;  (x === y) ## ​quasi-dereferenced variables'​ contents are type and value identical
 true true
  
Line 220: Line 221:
 ``` ```
  
-* PS: Julia also allows chained comparisons,​ like `1 < x <= 2 `.+* PS: Julia also allows ​[[controlflow#​multiple_conditions_(chained_comparisons)|chained comparisons,​ like `1 < x <= 2 `]] 
 + 
  
-FIXME (Andreas:) Not sure the use of *dereferenced* here is correct 
  
 ## Numeric Type Conversions ## Numeric Type Conversions
Line 396: Line 398:
  
 ```juliarepl ```juliarepl
-julia> ​isequal( ​(sqrt(5))^2 ​)+julia> (sqrt(5.0))^2 == 5.0
 false false
 ``` ```
Line 406: Line 408:
 5.000000000000001 5.000000000000001
 ``` ```
 +
 +
 +### Considering NaNs and Missing
 +
 +If missing or NaN are a concern, isequal functions slighly differently.
 +
 +```juliarepl
 +julia> isequal([1.,​ NaN], [1., NaN])
 +true
 +
 +julia> [1., NaN] == [1., NaN]
 +false
 +```
 +
 +- Unlike `==`, isequal does not consider -0.0 and +0.0 to be the same.
 +
 +
  
 ### Rough Comparing Floating-Point Numbers ### Rough Comparing Floating-Point Numbers
Line 435: Line 454:
  
 ``` ```
 +
 +* this also works for arrays and ranges: `sprintf1.("​%.3f",​ 1:3)` and `sprintf1.("​%.3f",​ [ 1 2 ; 3 4])`
  
  
Line 447: Line 468:
      ​3.14159      ​3.14159
 ``` ```
 +
 +* this also works for arrays and ranges: `sprintf1.("​%.3f",​ 1:3)` and `sprintf1.("​%.3f",​ [ 1 2 ; 3 4])`
 +
  
 ## Other Float Functions ## Other Float Functions
Line 461: Line 485:
 | `cospi(0.25)` ​ | `=cos(pi*0.25)`,​ here 0.707... ​ | | `cospi(0.25)` ​ | `=cos(pi*0.25)`,​ here 0.707... ​ |
 | `sind(270)` ​ | `= -1.0` input is in radians ​ | | `sind(270)` ​ | `= -1.0` input is in radians ​ |
 +
 +Many other useful functions are now defined in [SpecialFunctions.jl](http://​juliamath.github.io/​SpecialFunctions.jl/​latest/​). ​ For example,
 +
 +^ Function ​  ​^ ​ Explanation ​ ^
 +| `erf(x)` ​  | error function ​ |
 | `gamma(x)` ​  | gamma function ​ | | `gamma(x)` ​  | gamma function ​ |
 | `lgamma(x)` ​  | log gamma function ​ | | `lgamma(x)` ​  | log gamma function ​ |
Line 467: Line 496:
 | `lbeta(x,​y)` ​ | log beta function ​ | | `lbeta(x,​y)` ​ | log beta function ​ |
  
-FIXME (Andreas:) should probably mention that the last five are no longer in Base 
  
 # Integers and Integer-related Types # Integers and Integer-related Types
Line 525: Line 553:
 ``` ```
  
-You may or may not want to [[commandflow#​exceptions|trap]] these exceptions.+You may or may not want to [[controlflow#​exceptions|trap]] these exceptions.
  
  
Line 563: Line 591:
  
 An iterator is like a range of integers, typically used to index [[arraysintro|arrays]],​ [[dicts|dictionaries]],​ or other objects. ​ Thus, they are mostly discussed in the [[arraysvector#​range_iterators|Arrays Vector]] chapter. An iterator is like a range of integers, typically used to index [[arraysintro|arrays]],​ [[dicts|dictionaries]],​ or other objects. ​ Thus, they are mostly discussed in the [[arraysvector#​range_iterators|Arrays Vector]] chapter.
 +
  
  
 ## Iterating Over All Combinations / Permutations / etc. ## Iterating Over All Combinations / Permutations / etc.
  
-FIXME (Andreas:) Should probably mention ​https://​github.com/​JuliaMath/​Combinatorics.jl+* See also [Combinatorics.jl](https://​github.com/​JuliaMath/​Combinatorics.jl). 
  
 ```juliarepl ```juliarepl
Line 682: Line 712:
  
 ## Useful Packages on Julia Repository ## Useful Packages on Julia Repository
 +
 +* [Combinatorics.jl](https://​github.com/​JuliaMath/​Combinatorics.jl).
  
 ## Notes ## Notes
  
-* Unfortunately,​ it is not possible to turn on a compiler warning whenever an uninitialized variable is used before assignment.+* Unfortunately,​ it is not possible to turn on a compiler warning whenever an uninitialized variable is used before assignment. ​ (`x= Vector{Float64}(undef,​3);​ y= x[1]+2`.)
  
 * The above has not (yet) discussed constructs such as `x=Union{Int,​String}`,​ which would allow both `x=1` and `x="​hi"​`,​ but not `x=1.0`. ​ Such unions are especially important for dealing with [[missings|missing]] observations. ​ It has also not yet discussed`Any`,​ which is an extremely polygamous wildcard form of Union. * The above has not (yet) discussed constructs such as `x=Union{Int,​String}`,​ which would allow both `x=1` and `x="​hi"​`,​ but not `x=1.0`. ​ Such unions are especially important for dealing with [[missings|missing]] observations. ​ It has also not yet discussed`Any`,​ which is an extremely polygamous wildcard form of Union.
numbers.txt · Last modified: 2018/12/27 17:05 (external edit)