Python编程:科学计算与数值模拟入门

需积分: 9 45 下载量 102 浏览量 更新于2024-07-20 1 收藏 4.37MB PDF 举报
"Programming for Computations - Python" 是一本针对初学者的编程教材,专注于使用Python语言解决数学问题。本书的作者是Svein Linge和Hans Petter Langtangen,他们通过简洁易懂的方式介绍了科学计算的基础,旨在帮助没有编程经验的学生快速掌握编写程序来解决工程和科学课程中的数值方法问题。书中强调了通用算法、程序设计的清晰性、函数的使用以及自动测试来验证程序正确性的方法。 书的内容分为六个章节,涵盖了以下几个关键知识点: 1. **The First Few Steps**:这部分介绍了编程的基础,包括Python环境的设置,基本的数据类型(如整数、浮点数、字符串和布尔值),以及输入和输出操作。 2. **Basic Constructions**:这部分深入到控制流程,讲解了条件语句(if-else)和循环(for和while),这些都是编写任何程序的基础。此外,还介绍了函数的定义和调用,以及如何组织代码以提高可读性和可维护性。 3. **Computing Integrals**:在这一章,作者讲解了如何使用Python进行数值积分,这是许多物理和工程问题中的常见计算。这涉及到数值方法,如梯形法则、辛普森法则等。 4. **Solving Ordinary Differential Equations**:这部分介绍了解常微分方程(ODEs)的数值方法,如欧拉方法、龙格-库塔方法,这对于模拟动态系统至关重要。 5. **Solving Partial Differential Equations**:这部分扩展到偏微分方程(PDEs)的数值解,可能涉及有限差分法或有限元方法,这些方法广泛应用于流体动力学、热传导等领域。 6. 另外,书中还可能涵盖错误和异常处理、数据可视化、文件操作等主题,这些都是进行科学计算时不可或缺的部分。 这本书属于 Springer 的 "Texts in Computational Science and Engineering" 系列,适合工程和科学专业的学生,以及对使用Python进行数值计算感兴趣的读者。书中所提及的数学分类(2010年版)包括实分析、常微分方程、数值方法等领域,旨在通过编程实现对这些理论知识的实践应用。 通过阅读这本书,读者将获得编写Python程序来解决复杂数学问题的能力,这对于现代科学研究和工程实践至关重要。书中的实例和练习将帮助读者逐步建立对Python编程和数值方法的理解,进而能够进行简单的数值模拟和计算。

优化代码,GPU加速 def temp_condtion(df, temp_upper, temp_low): return ((df['max_temp']<=temp_upper) & (df['min_temp']>=temp_low)) def soc_condtion(df, soc_upper, soc_low): return ((df['bat_module_soc_00']<=temp_upper) & (df['bat_module_soc_00']>=temp_low)) def current_condtion(df, min_curr, batt_state): if batt_state=='charge': return (df['bat_module_current_00'].abs()>=min_curr) & (df['bat_module_current_00']>=0) elif batt_state=="discharge": return (df['bat_module_current_00'].abs()>=min_curr) & (df['bat_module_current_00']<=0 # 板端运行逻辑 data = {'realtime':[], 'cell_volt':[], 'total_current':[]} index = [] # (total_current[j]<=0) for i in tqdm(df.index[temp_condtion(df, temp_upper, temp_low) & soc_condtion(df, soc_upper, soc_low) & current_condtion(df, min_curr, 'discharge')]: n = 0 k = i while (n <= data_point) & (i <= len(df)-100): idx_list = [] idx_list.append(i) for j in np.arange(i+1, len(df)): if ((sendtime.iloc[j]-sendtime.iloc[k]).total_seconds()>=time_interval): break elif (df['max_temp'].iloc[j]<=temp_upper) & (df['min_temp'].iloc[j]>=temp_low) & \ (df['bat_module_soc_00'].iloc[j]>=soc_low) & (df['bat_module_soc_00'].iloc[j]<=soc_upper) & \ ((sendtime[j]-sendtime[i]).total_seconds()>=sample_interval) & \ ((sendtime.iloc[j]-sendtime.iloc[k]).total_seconds()<=time_interval) & \ (np.abs(total_current[j]-total_current[i])>=curr_interval) & (np.abs(soc[j]-soc[i])<=soc_interval) & \ (np.abs(total_current[j])>=min_curr): n+=1 idx_list.append(j) i = j if ((sendtime.iloc[j]-sendtime.iloc[k]).total_seconds()>=time_interval): break if len(idx_list) >= data_point: print(idx_list) index.append(idx_list)

2023-06-09 上传