agd.ExportedCode.Notebooks_Algo.TensorSelling
1# Code automatically exported from notebook TensorSelling.ipynb in directory Notebooks_Algo 2# Do not modify 3from ... import LinearParallel as lp 4from ... import FiniteDifferences as fd 5from ... import Selling 6from agd.Plotting import savefig; #savefig.dirName = 'Figures/TensorSelling' 7from ... import AutomaticDifferentiation as ad 8 9import numpy as np; xp = np; allclose = np.allclose 10import matplotlib.pyplot as plt 11 12def MakeRandomTensor(dim,shape = tuple(),relax=0.01): 13 identity = fd.as_field(np.eye(dim),shape,depth=2) 14 A = np.random.standard_normal( (dim,dim) + shape ) 15 M = lp.dot_AA(lp.transpose(A),A) + relax*identity 16 return xp.asarray(M) # Convert to GPU array if needed 17 18def Reconstruct(coefs,offsets): 19 return (coefs*lp.outer_self(offsets)).sum(2)
2171@array_function_dispatch(_allclose_dispatcher) 2172def allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False): 2173 """ 2174 Returns True if two arrays are element-wise equal within a tolerance. 2175 2176 The tolerance values are positive, typically very small numbers. The 2177 relative difference (`rtol` * abs(`b`)) and the absolute difference 2178 `atol` are added together to compare against the absolute difference 2179 between `a` and `b`. 2180 2181 NaNs are treated as equal if they are in the same place and if 2182 ``equal_nan=True``. Infs are treated as equal if they are in the same 2183 place and of the same sign in both arrays. 2184 2185 Parameters 2186 ---------- 2187 a, b : array_like 2188 Input arrays to compare. 2189 rtol : float 2190 The relative tolerance parameter (see Notes). 2191 atol : float 2192 The absolute tolerance parameter (see Notes). 2193 equal_nan : bool 2194 Whether to compare NaN's as equal. If True, NaN's in `a` will be 2195 considered equal to NaN's in `b` in the output array. 2196 2197 .. versionadded:: 1.10.0 2198 2199 Returns 2200 ------- 2201 allclose : bool 2202 Returns True if the two arrays are equal within the given 2203 tolerance; False otherwise. 2204 2205 See Also 2206 -------- 2207 isclose, all, any, equal 2208 2209 Notes 2210 ----- 2211 If the following equation is element-wise True, then allclose returns 2212 True. 2213 2214 absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`)) 2215 2216 The above equation is not symmetric in `a` and `b`, so that 2217 ``allclose(a, b)`` might be different from ``allclose(b, a)`` in 2218 some rare cases. 2219 2220 The comparison of `a` and `b` uses standard broadcasting, which 2221 means that `a` and `b` need not have the same shape in order for 2222 ``allclose(a, b)`` to evaluate to True. The same is true for 2223 `equal` but not `array_equal`. 2224 2225 `allclose` is not defined for non-numeric data types. 2226 `bool` is considered a numeric data-type for this purpose. 2227 2228 Examples 2229 -------- 2230 >>> np.allclose([1e10,1e-7], [1.00001e10,1e-8]) 2231 False 2232 >>> np.allclose([1e10,1e-8], [1.00001e10,1e-9]) 2233 True 2234 >>> np.allclose([1e10,1e-8], [1.0001e10,1e-9]) 2235 False 2236 >>> np.allclose([1.0, np.nan], [1.0, np.nan]) 2237 False 2238 >>> np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) 2239 True 2240 2241 """ 2242 res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan)) 2243 return bool(res)
Returns True if two arrays are element-wise equal within a tolerance.
The tolerance values are positive, typically very small numbers. The
relative difference (rtol
* abs(b
)) and the absolute difference
atol
are added together to compare against the absolute difference
between a
and b
.
NaNs are treated as equal if they are in the same place and if
equal_nan=True
. Infs are treated as equal if they are in the same
place and of the same sign in both arrays.
Parameters
a, b : array_like
Input arrays to compare.
rtol : float
The relative tolerance parameter (see Notes).
atol : float
The absolute tolerance parameter (see Notes).
equal_nan : bool
Whether to compare NaN's as equal. If True, NaN's in a
will be
considered equal to NaN's in b
in the output array.
*New in version 1.10.0.*
Returns
allclose : bool Returns True if the two arrays are equal within the given tolerance; False otherwise.
See Also
isclose, all, any, equal
Notes
If the following equation is element-wise True, then allclose returns True.
absolute(a
- b
) <= (atol
+ rtol
* absolute(b
))
The above equation is not symmetric in a
and b
, so that
allclose(a, b)
might be different from allclose(b, a)
in
some rare cases.
The comparison of a
and b
uses standard broadcasting, which
means that a
and b
need not have the same shape in order for
allclose(a, b)
to evaluate to True. The same is true for
equal
but not array_equal
.
allclose
is not defined for non-numeric data types.
bool
is considered a numeric data-type for this purpose.
Examples
>>> np.allclose([1e10,1e-7], [1.00001e10,1e-8])
False
>>> np.allclose([1e10,1e-8], [1.00001e10,1e-9])
True
>>> np.allclose([1e10,1e-8], [1.0001e10,1e-9])
False
>>> np.allclose([1.0, np.nan], [1.0, np.nan])
False
>>> np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
True