diff --git a/docs/source/reference/WorkingWithTypes.md b/docs/source/reference/WorkingWithTypes.md index 0fc79ecdbfa4afd80cf792e2d0445c15dbf1ed43..c7e3fb5cacd7448717f2e47ebe8d75f1836406aa 100644 --- a/docs/source/reference/WorkingWithTypes.md +++ b/docs/source/reference/WorkingWithTypes.md @@ -23,8 +23,30 @@ import pystencils as ps import sympy as sp ``` +## Changing the Default Data Types + +The pystencils code generator defines two default data types: + - The default *numeric type*, which is applied to all numerical computations that are not + otherwise explicitly typed; the default is `float64`. + - The default *index type*, which is used for all loop and field index calculations; the default is `int64`. + +These can be modified by setting the +{any}`default_dtype <CreateKernelConfig.default_dtype>` and +{any}`index_type <CreateKernelConfig.index_dtype>` +options of the code generator configuration: + +```{code-cell} ipython3 +cfg = ps.CreateKernelConfig() +cfg.default_dtype = "float32" +cfg.index_dtype = "int32" +``` + +Modifying these will change the way types for [untyped symbols](#untyped-symbols) +and [dynamically typed expressions](#dynamic-typing) are computed. + ## Setting the Types of Fields and Symbols +(untyped-symbols)= ### Untyped Symbols Symbols used inside a kernel are most commonly created using @@ -65,6 +87,7 @@ str(f.dtype), str(g.dtype) When using `Field.create_generic` or `Field.create_fixed_size`, on the other hand, you can set the data type via the `dtype` keyword argument. +(dynamic-typing)= ### Dynamically Typed Symbols and Fields Apart from explicitly setting data types, @@ -92,9 +115,11 @@ if any type conflicts arise, it will terminate with an error. Still, there are cases where you want to combine subexpressions of different types; maybe you need to compute geometric information from loop counters or other integers, or you are doing mixed-precision numerical computations. -In these cases, you might have to - 1. Introduce explicit type casts when values move from one type context to another; - 2. Annotate expressions with a specific data type to ensure computations are performed in that type. +In these cases, you might have to introduce explicit type casts when values move from one type context to another. + + <!-- 2. Annotate expressions with a specific data type to ensure computations are performed in that type. + TODO: See #97 (https://i10git.cs.fau.de/pycodegen/pystencils/-/issues/97) + --> (type_casts)= ### Type Casts @@ -116,11 +141,6 @@ and then introduce a runtime cast to the target type. That target type must comply with the type computed for the outer expression, which the cast is embedded in. -(explicit_expression_types)= -### Setting Explicit Types for Expressions - - - ## Understanding the pystencils Type Inference System To correctly apply varying data types to pystencils kernels, it is important to understand diff --git a/src/pystencils/field.py b/src/pystencils/field.py index c64e0afad89f6d25a98c2566fb33b2744f767c38..a4c2712a62dece39086a3ba2def1a2ce85e1f2b5 100644 --- a/src/pystencils/field.py +++ b/src/pystencils/field.py @@ -7,7 +7,7 @@ import pickle import re from enum import Enum from itertools import chain -from typing import List, Optional, Sequence, Set, Tuple, Union +from typing import List, Optional, Sequence, Set, Tuple from warnings import warn import numpy as np