具有自动域的数学函数( numpy.emath / numpy.lib.scimath)

本文介绍了numpy.emath和numpy.lib.scimath模块,这两个模块提供了自动处理复数域的数学函数。通过示例,解释了在实数域中对负数开平方会得到复数结果,帮助理解数学函数如何在不同域中转换。同时,讨论了函数的‘分支切割’概念,即函数在复数域的行为,建议通过绘制复数域图像以增进理解。

最近看到一个神奇的函数 numpy.emath
看了官方文档不知道说什么

先看一下官方文档:

注意 numpy.emathnumpy.lib.scimath 的首选别名, 在导入 numpy 后可用。
包装器函数对某些数学函数的调用更加用户友好,这些数学函数的输出数据类型与输入的某些域中的输入数据类型不同。

例如,对于带有分支切割的 log 之类的功能,此模块中的版本在复杂平面中提供数学上有效的答案:

import math
 from numpy.lib import scimath
 scimath.log(-math.exp(1)) == (
from math import * import numpy as np import pandas as pd import os #权重文件位置 WEIGHTS_JSON="../result/weight.json" #采样次数 SAMPLE_TIMES=100 #Fbwm输入目录 FBWM_INPUT_DIR="../input/fuzzy_bwm_input/" #Fbwm输入文件 FBWM_INPUT_PATH=os.path.join(FBWM_INPUT_DIR,"c4_c5_fuzzy_bwm.xlsx") #权重方法 WEIGHTMETHOD="bwm" #结果目录 RESULT_DIR="../result/analysis/mc/tri_dis/" #攻击数量 #初始化结果目录 os.makedirs(RESULT_DIR,exist_ok=True)#递归创建,已存在则跳过 #清空目录文件 for file in os.listdir(RESULT_DIR): file_path=os.path.join(RESULT_DIR,file) try: os.remove(file_path) except OSError as e: print(f"无法删除旧文件{file_path},原因{e}") #逆变换采样函数 def c_tfn(tfn): l,m,u=tfn if not (l<m<u): raise ValueError("tfn格式错误") r=np.random.rand()#生成[0,1]之间的随机数 p=(m-l)/(u-l) if r<p: return l+sqrt(r*(u-l)*(m-l)) else: return u-sqrt((1-r)(u-m)(u-l)) #读取权重评估数据 address=FBWM_INPUT_PATH df=pd.read_excel(address, sheet_name = "Sheet1", skiprows = 2, nrows= 9, usecols=[1,2,3],header=None) df.columns=["Criteria","Best","Worst"] Best = df["Criteria"][df[df['Best'] == "Equally importance"].index.tolist()[0]] Worst = df["Criteria"][df[df['Worst'] == "Equally importance"].index.tolist()[0]] df.columns = ["Criteria", Best, Worst] Cnum = df.shape[0] df.set_index(df["Criteria"], inplace=True) # Fuzzification Fuzzy = {"Equally importance": [1, 1, 1], "Weakly important": [2 / 3, 1, 3 / 2], "Fairly important": [3 / 2, 2, 5 / 2], "Very important": [5 / 2, 3, 7 / 2], "Absolutely important": [7 / 2, 4, 9 / 2]} #确认输出 mc_bwminput_output_path=os.path.join(RESULT_DIR,"mc_bwm_input.xlsx") result={} #蒙特卡洛循环 for sample_idx in range(SAMPLE_TIMES): for i in [Best, Worst]: for j in range(df.shape[0]): df[i][j] =c_tfn(Fuzzy[df[i][j]])#随机采样 df.to_excel(mc_bwminput_output_path,index=False)#随机采样后的bwm输入 weightmethod=WEIGHTMETHOD #权重计算 os.system('/usr/bin/time -f"%e %M" -a -o ../result/tmp/time.log python3 ../lib/Weight/{}/{}.py {} {}'.format(weightmethod , weightmethod , mc_bwminput_output_path, '../result/weight.json')) #指标计算 #os.system('/usr/bin/time -f"%e %M" -a -o ../result/tmp/time.log python3 ../lib/cal_metrics.py {} {}'.format('../result/tmp/topo.json', '../result/path_metrics.csv')) #排名计算 os.system('/usr/bin/time -f"%e %M" -a -o ../result/tmp/time.log python3 ../lib/Rank/{}/{}.py {} {} {} {} {}'.format('mabac' , 'mabac' , '../result/tmp/topo.json',WEIGHTS_JSON, '../result/tmp/data.csv', '../result/rank.json', '../result/path_metrics.csv')) try: with open('../result/rank.json',"r") as f: rank=json.load(f)#读取排名文件 with open(WEIGHTS_JSON,"r") as m: weight=json.load(m)#读取权重文件 except Exception as e: print(f"读取出错:{e}") #整合输出 result[sample_idx]={ "weight":weight[weight], "TFN":weight[TFN], "ksi":weight[ksi], "CR":weight[CR], "ranking":rank[ranking] } #输出排序结果 output_path=os.path.join(RESULT_DIR,"monte_carlo_tfn.json") with open(output_path,"w") as f: json.dump(result,f,indent=2) 我的代码功能可以实现嘛
最新发布
08-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值