1# Copyright 2022 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15 16import numpy as np 17import pytest 18 19import mindspore.context as context 20from mindspore import Tensor 21import mindspore.ops as F 22 23 24@pytest.mark.level0 25@pytest.mark.platform_x86_cpu 26@pytest.mark.env_onecard 27@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 28def test_bessel_j0(dtype, eps): 29 """ 30 Feature: bessel j0 function 31 Description: test cases for BesselJ0 32 Expectation: the result matches scipy 33 """ 34 x = Tensor(np.array([0.5, 1., 2., 4.]).astype(dtype)) 35 expect = np.array( 36 [0.9384698, 0.7651977, 0.22389078, -0.3971498]).astype(dtype) 37 error = np.ones(shape=[4]) * eps 38 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 39 40 output = F.bessel_j0(x) 41 diff = np.abs(output.asnumpy() - expect) 42 assert np.all(diff < error) 43 44 45@pytest.mark.level0 46@pytest.mark.platform_x86_cpu 47@pytest.mark.env_onecard 48@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 49def test_bessel_j1(dtype, eps): 50 """ 51 Feature: bessel j1 function 52 Description: test cases for BesselJ1 53 Expectation: the result matches scipy 54 """ 55 x = Tensor(np.array([0.5, 1., 2., 4.]).astype(dtype)) 56 expect = np.array( 57 [0.24226846, 0.44005057, 0.5767248, -0.06604332]).astype(dtype) 58 error = np.ones(shape=[4]) * eps 59 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 60 61 output = F.bessel_j1(x) 62 diff = np.abs(output.asnumpy() - expect) 63 assert np.all(diff < error) 64 65 66@pytest.mark.level0 67@pytest.mark.platform_x86_cpu 68@pytest.mark.platform_arm_ascend_training 69@pytest.mark.platform_x86_ascend_training 70@pytest.mark.env_onecard 71@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 72def test_bessel_i0(dtype, eps): 73 """ 74 Feature: bessel i0 function 75 Description: test cases for BesselI0 76 Expectation: the result matches scipy 77 """ 78 x = Tensor(np.array([-1, -0.5, 0.5, 1]).astype(dtype)) 79 expect = np.array( 80 [1.2660658, 1.0634834, 1.0634834, 1.2660658]).astype(dtype) 81 error = np.ones(shape=[4]) * eps 82 context.set_context(mode=context.GRAPH_MODE) 83 84 output = F.bessel_i0(x) 85 diff = np.abs(output.asnumpy() - expect) 86 assert np.all(diff < error) 87 88 89@pytest.mark.level0 90@pytest.mark.platform_x86_cpu 91@pytest.mark.env_onecard 92@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 93def test_bessel_i0e(dtype, eps): 94 """ 95 Feature: bessel i0e function 96 Description: test cases for BesselI0e 97 Expectation: the result matches scipy 98 """ 99 x = Tensor(np.array([-1, -0.5, 0.5, 1]).astype(dtype)) 100 expect = np.array( 101 [0.4657596, 0.64503527, 0.64503527, 0.4657596]).astype(dtype) 102 error = np.ones(shape=[4]) * eps 103 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 104 105 output = F.bessel_i0e(x) 106 diff = np.abs(output.asnumpy() - expect) 107 assert np.all(diff < error) 108 109 110@pytest.mark.level0 111@pytest.mark.platform_x86_cpu 112@pytest.mark.env_onecard 113@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 114def test_bessel_k0(dtype, eps): 115 """ 116 Feature: bessel k0 function 117 Description: test cases for BesselK0 118 Expectation: the result matches scipy 119 """ 120 x = Tensor(np.array([0.5, 1., 2., 4.]).astype(dtype)) 121 expect = np.array( 122 [0.92441905, 0.42102444, 0.11389387, 0.01115968]).astype(dtype) 123 error = np.ones(shape=[4]) * eps 124 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 125 126 output = F.bessel_k0(x) 127 diff = np.abs(output.asnumpy() - expect) 128 assert np.all(diff < error) 129 130 131@pytest.mark.level0 132@pytest.mark.platform_x86_cpu 133@pytest.mark.env_onecard 134@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 135def test_bessel_k0e(dtype, eps): 136 """ 137 Feature: bessel k-e function 138 Description: test cases for BesselK0e 139 Expectation: the result matches scipy 140 """ 141 x = Tensor(np.array([0.5, 1., 2., 4.]).astype(dtype)) 142 expect = np.array( 143 [1.5241094, 1.1444631, 0.84156823, 0.6092977]).astype(dtype) 144 error = np.ones(shape=[4]) * eps 145 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 146 147 output = F.bessel_k0e(x) 148 diff = np.abs(output.asnumpy() - expect) 149 assert np.all(diff < error) 150 151 152@pytest.mark.level0 153@pytest.mark.platform_x86_cpu 154@pytest.mark.env_onecard 155@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 156def test_bessel_y0(dtype, eps): 157 """ 158 Feature: bessel y0 function 159 Description: test cases for BesselY0 160 Expectation: the result matches scipy 161 """ 162 x = Tensor(np.array([0.5, 1., 2., 4.]).astype(dtype)) 163 expect = np.array( 164 [-0.44451874, 0.08825696, 0.51037567, -0.01694074]).astype(dtype) 165 error = np.ones(shape=[4]) * eps 166 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 167 168 output = F.bessel_y0(x) 169 diff = np.abs(output.asnumpy() - expect) 170 assert np.all(diff < error) 171 172 173@pytest.mark.level0 174@pytest.mark.platform_x86_cpu 175@pytest.mark.env_onecard 176@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 177def test_bessel_y1(dtype, eps): 178 """ 179 Feature: bessel y1 function 180 Description: test cases for BesselY1 181 Expectation: the result matches scipy 182 """ 183 x = Tensor(np.array([0.5, 1., 2., 4.]).astype(dtype)) 184 expect = np.array([-1.47147239, -0.78121282, 185 -0.10703243, 0.39792571]).astype(dtype) 186 error = np.ones(shape=[4]) * eps 187 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 188 189 output = F.bessel_y1(x) 190 diff = np.abs(output.asnumpy() - expect) 191 assert np.all(diff < error) 192 193 194@pytest.mark.level0 195@pytest.mark.platform_x86_cpu 196@pytest.mark.env_onecard 197@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 198def test_bessel_i1(dtype, eps): 199 """ 200 Feature: bessel i1 function 201 Description: test cases for BesselI1 202 Expectation: the result matches scipy 203 """ 204 x = Tensor(np.array([-1, -0.5, 0.5, 1]).astype(dtype)) 205 expect = np.array( 206 [-0.5651591, -0.25789431, 0.25789431, 0.5651591]).astype(dtype) 207 error = np.ones(shape=[4]) * eps 208 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 209 210 output = F.bessel_i1(x) 211 diff = np.abs(output.asnumpy() - expect) 212 assert np.all(diff < error) 213 214 215@pytest.mark.level0 216@pytest.mark.platform_x86_cpu 217@pytest.mark.env_onecard 218@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 219def test_bessel_i1e(dtype, eps): 220 """ 221 Feature: bessel i1e function 222 Description: test cases for BesselI1e 223 Expectation: the result matches scipy 224 """ 225 x = Tensor(np.array([-1, -0.5, 0.5, 1]).astype(dtype)) 226 expect = np.array( 227 [-0.20791042, -0.15642083, 0.15642083, 0.20791042]).astype(dtype) 228 error = np.ones(shape=[4]) * eps 229 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 230 231 output = F.bessel_i1e(x) 232 diff = np.abs(output.asnumpy() - expect) 233 assert np.all(diff < error) 234 235 236@pytest.mark.level0 237@pytest.mark.platform_x86_cpu 238@pytest.mark.env_onecard 239@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 240def test_bessel_k1(dtype, eps): 241 """ 242 Feature: bessel k1 function 243 Description: test cases for BesselK1 244 Expectation: the result matches scipy 245 """ 246 x = Tensor(np.array([0.5, 1., 2., 4.]).astype(dtype)) 247 expect = np.array( 248 [1.65644112, 0.60190723, 0.13986588, 0.0124835]).astype(dtype) 249 error = np.ones(shape=[4]) * eps 250 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 251 252 output = F.bessel_k1(x) 253 diff = np.abs(output.asnumpy() - expect) 254 assert np.all(diff < error) 255 256 257@pytest.mark.level0 258@pytest.mark.platform_x86_cpu 259@pytest.mark.env_onecard 260@pytest.mark.parametrize('dtype, eps', [(np.float16, 1.0e-3), (np.float32, 1.0e-6), (np.float64, 1.0e-6)]) 261def test_bessel_k1e(dtype, eps): 262 """ 263 Feature: bessel k1e function 264 Description: test cases for BesselK1e 265 Expectation: the result matches scipy 266 """ 267 x = Tensor(np.array([0.5, 1., 2., 4.]).astype(dtype)) 268 expect = np.array( 269 [2.73100971, 1.63615349, 1.03347685, 0.68157595]).astype(dtype) 270 error = np.ones(shape=[4]) * eps 271 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 272 273 output = F.bessel_k1e(x) 274 diff = np.abs(output.asnumpy() - expect) 275 assert np.all(diff < error) 276