天天看点

覃学苦练(八):Mathematica编程中程序包的定义与使用

覃学苦练(八):Mathematica编程中程序包的定义与使用

分享兴趣,传播快乐,

增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。

今天小编为大家带来文章

“覃学苦练(八):Mathematica编程中程序包的定义与使用”

欢迎您的访问。

Share interest,spread happiness,

increase knowledge, leave a beautiful!

Dear, this is LearningYard Academy.

Today, the editor brings you an article"Qin Xue's hard training (8): The Definition and Use of Package in Mathematica Programming "

Welcome your visit.

一、思维导图(Mind mapping)

覃学苦练(八):Mathematica编程中程序包的定义与使用

二、程序包(Package)

Mathematica编程中的程序包就是一些功能相近的函数语句的集合,按照某种方式组合起来以便用户使用,相当于面向对象程序设计中的类。当使用时,系统会自动加载一些软件包,可以通过“$Packages”系统变量知道哪些程序包已经被加载,通过系统变量“$Path”知道这些程序包的位置。如下图所示:

A package in Mathematica programming is a collection of functional statements with similar functions, which are combined in some way for users to use, equivalent to a class in object-oriented programming. When using, the system will automatically load some packages. You can know which packages have been loaded through the " $Packages" system variable, and know the location of these packages through the " $Path" system variable. As shown in the following figure:

覃学苦练(八):Mathematica编程中程序包的定义与使用
覃学苦练(八):Mathematica编程中程序包的定义与使用

三、程序包的自定义(Customization of the package)

1、新建Wolfram语言程序包(New Wolfram Language Package)

打开Mathematica软件,在菜单栏中选择“文件”,选择“新建”然后选择“程序包\脚本”在其中选择“Wolfram语言程序包”就新建了一个Wolfram语言程序包。可以自己命名程序包名称,以便后续引入使用程序。

Open the Mathematica software, select "File" in the menu bar, select "New" and then select "Package \ Script", and select "Wolfram Language Package" to create a new Wolfram Language Package. You can name the package name yourself so that you can use it later.

覃学苦练(八):Mathematica编程中程序包的定义与使用
覃学苦练(八):Mathematica编程中程序包的定义与使用

在程序包定义结构中,BeginPackage[“程序包名称”]与EndPackage[]定义程序包的上下文,Begin[“Private`”]与End[]设置了当前环境上下文。通过以上设置上下文来区别不同环境下的同名变量,若特定的名字出现在一个上下文中,则没必要明确上下文的名字了。程序包中通过f::usage语句定义函数f的使用信息,对函数进行描述和用法说明。

In the package definition structure, BeginPackage [ "PackageName"] and EndPackage [] define the context of the package, and Begin [ "Private`"] and End [] set the current environment context. Variables with the same name in different environments can be distinguished by setting the context above. If a specific name appears in a context, there is no need to specify the name of the context. The package defines the usage information of the function f through the f: usage statement, and describes the function and its usage.

四、程序包的使用(Use of packages)

1、程序包导入(Package import)

当程序定义完成后,点击保存,可自由保存到自己想保存的位置,然后在Mathematica的笔记本窗体可使用Get[“文件浏览器”]选择自己保存的程序包位置导入程序包执行其中代码。

After the program definition is completed, click Save to freely save to the location you want to save, and then use Get [ "File Browser"] to select the location of the package you want to save in the notebook form of Mathematica to import the package and execute the code.

2、编程实例(Programming example)

根据以上阐述,小编通过一道简单实例来给大家演示一下全过程的操作。实例:生成一个软件包,用来求矩形的面积和周长。如下图所示:

According to the above elaboration, Xiaobian demonstrates the operation of the whole process through a simple example. Example: Generate a package to find the area and perimeter of a rectangle. As shown in the following figure:

覃学苦练(八):Mathematica编程中程序包的定义与使用

在程序自定义输入时可用快捷键“Ait+Enter”新建一个Cell;并可通过“Definition[f]”函数或“?f”来查看自定义函数的信息。

You can use the shortcut key "Ait + Enter" to create a new Cell during the custom input of the program, and you can use the "Definition [f]" function or "? F "to view the information of the custom function."

今天的分享就到这里了。

如果您对文章有独特的想法,

欢迎给我们留言,

让我们相约明天。

祝您今天过得开心快乐!

That's all for today's sharing.

If youhave a unique idea about the article,

please leave us a message,

Let us meet tomorrow.

I wish you a happy day today!

参考资料:

翻译:讯飞星火翻译,《Mathematica基础及其在数学建模中的应用》

本文由LearningYard新学苑整理发出,如有侵权请在后台留言沟通!

文案:qin

排版:qin

审核|Yue

继续阅读