Experimental(实验性 API) 模块¶
Experimental
Experimental 模块下均为实验性 API,其签名和位置在未来可能发生变动
ppsci.experimental.math_module
¶
bessel_i0(x)
¶
Zero-order modified Bézier curve functions of the first kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input data of the formula. |
required |
Examples:
>>> import paddle
>>> import ppsci
>>> res = ppsci.experimental.bessel_i0(paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32"))
Source code in ppsci/experimental/math_module.py
bessel_i0e(x)
¶
Exponentially scaled zero-order modified Bézier curve functions of the first kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input data of the formula. |
required |
Examples:
>>> import paddle
>>> import ppsci
>>> res = ppsci.experimental.bessel_i0e(paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32"))
Source code in ppsci/experimental/math_module.py
bessel_i1(x)
¶
First-order modified Bézier curve functions of the first kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input data of the formula. |
required |
Examples:
>>> import paddle
>>> import ppsci
>>> res = ppsci.experimental.bessel_i1(paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32"))
Source code in ppsci/experimental/math_module.py
bessel_i1e(x)
¶
Exponentially scaled first-order modified Bézier curve functions of the first kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input data of the formula. |
required |
Examples:
>>> import paddle
>>> import ppsci
>>> res = ppsci.experimental.bessel_i1e(paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32"))
Source code in ppsci/experimental/math_module.py
fractional_diff(func, alpha, a, t, h, dtype='float64')
¶
Compute fractional derivative of given function at point t with fractional order alpha using Caputo derivative of fractional.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Function to compute the fractional derivative of. |
required |
alpha
|
float
|
Fractional order. |
required |
t
|
float
|
Point to compute the fractional derivative at. |
required |
a
|
float
|
Start point of the fractional integral. |
required |
h
|
float
|
Step size for finite difference. |
required |
dtype
|
str
|
Data dtype during computation. Defaults to "float64". |
'float64'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
paddle.Tensor: Fractional derivative result of the function at t. |
Examples:
>>> from ppsci.experimental import fractional_diff
>>> import numpy as np
>>> # define f(x) = x^2
>>> def f(x):
... return x * x
>>> # compute 0.5-order fractional derivative of f(x) at t=1.0 with step size h=1e-6
>>> res = fractional_diff(f, alpha=0.5, a=0, t=1.0, h=1e-6, dtype="float64")
>>> np.testing.assert_allclose(float(res), 1.503547, 1e-6)
Source code in ppsci/experimental/math_module.py
gaussian_integrate(fn, dim, N, integration_domains, dtype='float64')
¶
Integrate given function using gaussian quadrature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[[Any], Tensor]
|
Function to be integrated. |
required |
dim
|
int
|
Dimensionality of the integrand. |
required |
N
|
int
|
Number of dicretization points. |
required |
integration_domains
|
List[List[float]]
|
Integration domains. |
required |
dtype
|
Literal['float32', 'float64']
|
Dtype used during computation. Defaults to "float64". |
'float64'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
paddle.Tensor: Integral result. |
Examples:
>>> import numpy as np
>>> import paddle
>>> import ppsci.experimental
>>> func = lambda x: paddle.sin(x)
>>> dim = 1
>>> N = 500
>>> integration_domains = [[0, np.pi]]
>>> result = ppsci.experimental.gaussian_integrate(func, dim, N, integration_domains)
>>> np.testing.assert_allclose(float(result), 2.0, 1e-6)
>>> print(float(result))
1.9999999999999576
Source code in ppsci/experimental/math_module.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
montecarlo_integrate(fn, dim, N=1000, integration_domain=None, seed=None)
¶
Integrates the passed function on the passed domain using vanilla Monte Carlo Integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable
|
The function to integrate over. |
required |
dim
|
int
|
Dimensionality of the function's domain over which to integrate. |
required |
N
|
int
|
Number of sample points to use for the integration. Defaults to 1000. |
1000
|
integration_domain
|
Optional[Union[List[List[float]], Tensor]]
|
Integration domain, e.g. [[-1,1],[0,1]]. Defaults to [-1,1]^dim. |
None
|
seed
|
Optional[int]
|
Random number generation seed to the sampling point creation, only set if provided. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If len(integration_domain) != dim |
Returns:
| Type | Description |
|---|---|
Tensor
|
paddle.Tensor: Integral result. |
Examples:
>>> _ = paddle.seed(1024)
>>> # The function we want to integrate, in this example
>>> # f(x0,x1) = sin(x0) + e^x1 for x0=[0,1] and x1=[-1,1]
>>> # Note that the function needs to support multiple evaluations at once (first
>>> # dimension of x here)
>>> # Expected result here is ~3.2698
>>> def some_function(x):
... return paddle.sin(x[:, 0]) + paddle.exp(x[:, 1])
>>> # Compute the function integral by sampling 10000 points over domain
>>> integral_value = ppsci.experimental.montecarlo_integrate(
... some_function,
... dim=2,
... N=10000,
... integration_domain=[[0, 1], [-1, 1]],
... )
>>> print(integral_value)
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
3.25152588)
Source code in ppsci/experimental/math_module.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
trapezoid_integrate(y, x=None, dx=None, axis=-1, mode='sum')
¶
Integrate along the given axis using the composite trapezoidal rule. Use the sum method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
Tensor
|
Input to be integrated. |
required |
x
|
Tensor
|
The sample points corresponding to the input samples. its shape should be (1) input.shape; (2) the input.shape[axis] if axis is not default. Defaults to None. dx (float, optional): The sample points are assumed to be evenly spaced and it is the spacing between sample points. If 'x' and 'dx' are both default, 'dx' is set to 1 by default. Defaults to None. |
None
|
axis
|
int
|
The axis along which to integrate. Defaults to -1. |
-1
|
mode
|
Literal['sum', 'cumsum']
|
Which type cumulative sum function used. Defaults to "sum". |
'sum'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
paddle.Tensor: Integral result. If dim of input is N, return is N-1 dim. |
Examples:
>>> import paddle
>>> import ppsci
>>> y = paddle.to_tensor([[0, 1, 2], [3, 4, 5]], dtype="float32")
>>> res = ppsci.experimental.trapezoid_integrate(y)
>>> print(res)
Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[2., 8.])
>>> res = ppsci.experimental.trapezoid_integrate(y, mode="cumsum")
>>> print(res)
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[0.50000000, 2. ],
[3.50000000, 8. ]])
>>> res = ppsci.experimental.trapezoid_integrate(
... y, x=paddle.to_tensor([[0, 1, 2], [3, 4, 5]], dtype="float32")
... )
>>> print(res)
Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[2., 8.])
>>> res = ppsci.experimental.trapezoid_integrate(
... y, x=paddle.to_tensor([0, 1], dtype="float32"), axis=0
... )
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[1.50000000, 2.50000000, 3.50000000])
>>> res = ppsci.experimental.trapezoid_integrate(
... y, x=paddle.to_tensor([0, 1, 2], dtype="float32"), axis=1
... )
>>> print(res)
Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[2., 8.])
>>> res = ppsci.experimental.trapezoid_integrate(y, dx=2)
>>> print(res)
Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[4. , 16.])