📜 ⬆️ ⬇️

Wolfram Language (Mathematica) in Russian ... or advanced function assignment


Download the post in the form of a Mathematica document, which contains all the code used, here .

One of the most important skills in working with the Mathematica system is the assignment of functions that would have the most different forms, and would depend on a different number of variables (from literally no variable to an infinite number of variables), while some variables could have values which are used by default if their specific values ​​are not entered, others would have the appearance of options, like many functions built into Mathematica , or would have strict restrictions on their type ...

This post will show you how to program in Mathematica in Russian, and for this I will show you how to create functions whose names are given in Cyrillic, corresponding to the original built-in functions. In this case, you will learn how to actually set the most different types and types of functions.
')
I would like to note that in version 10.1 of the Wolfram Language ( Mathematica ), the process of Russification of the interface, documentation and predictive interface began. For a number of languages, for example, Chinese, the process of full localization is almost complete. Moreover, even Wolfram | Alpha will soon be able to work in Chinese.

WolframLanguageInRussian) rAdvancedFunctionSetting_1.gif

Creating standard functions of several variables


Deferred function setting


You can set a standard function using the following structure:

WolframLanguageInRussian) rAdvancedFunctionSetting_2.png

This construction involves three extremely important Mathematica functions, the first is the delayed assignment SetDelayed (in short form it looks like = ), the second is the template expression Blank [] (in short form it looks like _ ), the third is the template with the name Pattern assigned to it [name, object] (in short form has the form name: object , or, if the template object is simply _ , then name_ ).

Let us consider in more detail why they are needed and how they work.



WolframLanguageInRussian) rAdvancedFunctionSetting_3.gif

it assigns the expression rhs (short for “right hand side” to the right) as a delayed value of the object (symbol) lhs (short for left hand to hand is left), while the expression rhs remains in an uncalculated form. As soon as the lhs object is found in the code, it is replaced with the rhs expression, which is then evaluated.



WolframLanguageInRussian) rAdvancedFunctionSetting_4.gif

or

WolframLanguageInRussian) rAdvancedFunctionSetting_5.gif

it represents any expression written in the Wolfram Language. In this case, the _h pattern specifies any expression whose head part matches h .



WolframLanguageInRussian) rAdvancedFunctionSetting_6.gif

or

WolframLanguageInRussian) rAdvancedFunctionSetting_7.gif

it serves to “transfer” the object inside the functional structures used (replacement rules, setting functions, etc.)

Well, summing up the above, we can say that the standard definition of the function of one variable is:

WolframLanguageInRussian) rAdvancedFunctionSetting_8.png

which in its full form (in the one that actually “sees” it and uses Mathematica ) looks like this:

WolframLanguageInRussian) rAdvancedFunctionSetting_9.png

Setting a function in the form of a deferred assignment, which in general has the form:

f [pattern]: = lhs

works as follows:



It is clear that if you use a function of the form f [v _]: = lhs [v] , then, as mentioned above, any expression expr will satisfy the pattern v_ .

I will give an example. Define a function that calculates WolframLanguageInRussian) rAdvancedFunctionSetting_10.png :

WolframLanguageInRussian) rAdvancedFunctionSetting_11.png

Let's try to calculate its value from different arguments:

WolframLanguageInRussian) rAdvancedFunctionSetting_12.png

WolframLanguageInRussian) rAdvancedFunctionSetting_13.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_14.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_15.gif

As can be seen from the examples discussed above, indeed any object (number, symbol, picture) satisfies the x_ pattern and Mathematica works easily with all expressions, which demonstrates the extraordinary flexibility of its language.

Setting the function of many variables is completely analogous. Let's say we give the function 3 variables:

WolframLanguageInRussian) rAdvancedFunctionSetting_16.png

WolframLanguageInRussian) rAdvancedFunctionSetting_17.png

WolframLanguageInRussian) rAdvancedFunctionSetting_18.gif

Creating a function that “remembers” its previously calculated values


If you change the deferred function setting as follows:

WolframLanguageInRussian) rAdvancedFunctionSetting_19.png

then once having calculated the value at some “point” (the value for a certain value of the argument, the argument may not be a number and not necessarily a point in its usual sense ...), the function f will remember (though only for this Mathematica session) this value ( here the Set function was used (in short form it looks like = ), which allows assigning some value to some symbol).

Such a function assignment can save a lot of time, if the calculation of the right side of the delayed assignment of a function takes a lot of time, but it requires more memory.

An example (the time to calculate a certain command can be found using the Timing function):

WolframLanguageInRussian) rAdvancedFunctionSetting_20.png

WolframLanguageInRussian) rAdvancedFunctionSetting_21.png

WolframLanguageInRussian) rAdvancedFunctionSetting_22.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_23.png

WolframLanguageInRussian) rAdvancedFunctionSetting_24.gif

From the example it can be seen that once having calculated the value of a function at a point, with this setting of the function, next time it will not take any time.



WolframLanguageInRussian) rAdvancedFunctionSetting_25.png

WolframLanguageInRussian) rAdvancedFunctionSetting_26.png

WolframLanguageInRussian) rAdvancedFunctionSetting_27.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_28.png

WolframLanguageInRussian) rAdvancedFunctionSetting_29.png

WolframLanguageInRussian) rAdvancedFunctionSetting_30.gif

Absolute function assignment


If, instead of using the SetDelayed ( : = ) function, you use the Set ( = ) function in the function definition:

WolframLanguageInRussian) rAdvancedFunctionSetting_31.png

then the right expression lhs [v] will be calculated immediately, after which the values ​​of the function arguments will be substituted into it when a further expression of the form f [expr] is encountered in the code. This form of setting a function can save time if the evaluation of the expression lhs [v] is laborious and, in fact, does not depend on the specific value of the argument v .

Compare the deferred and absolute assignments of functions.

The calculation of the lhs [v] expression does not depend on the value of v directly:

WolframLanguageInRussian) rAdvancedFunctionSetting_32.png

WolframLanguageInRussian) rAdvancedFunctionSetting_33.png

WolframLanguageInRussian) rAdvancedFunctionSetting_34.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_35.png

WolframLanguageInRussian) rAdvancedFunctionSetting_36.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_37.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_38.gif

The calculation of the lhs [v] expression depends on the value of v directly:

WolframLanguageInRussian) rAdvancedFunctionSetting_40.png

WolframLanguageInRussian) rAdvancedFunctionSetting_41.png

WolframLanguageInRussian) rAdvancedFunctionSetting_42.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_43.png

WolframLanguageInRussian) rAdvancedFunctionSetting_44.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_45.png

WolframLanguageInRussian) rAdvancedFunctionSetting_46.gif

From the two examples above, two important conclusions can be drawn:





Setting a function, some arguments of which have default values


In some situations it is convenient to use functions, some variables of which would have default values. You can set a variable of this kind for a function using the Optional function, which is used as follows:

WolframLanguageInRussian) rAdvancedFunctionSetting_47.gif

Let us set a function that builds a circle or a circle, while by default the center of this circle (circle) is at the point (0, 0), it (she) has a radius of 1, and also has a red color:

WolframLanguageInRussian) rAdvancedFunctionSetting_48.png

WolframLanguageInRussian) rAdvancedFunctionSetting_49.png

WolframLanguageInRussian) rAdvancedFunctionSetting_50.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_51.png

WolframLanguageInRussian) rAdvancedFunctionSetting_52.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_53.png

WolframLanguageInRussian) rAdvancedFunctionSetting_54.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_55.png

WolframLanguageInRussian) rAdvancedFunctionSetting_56.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_57.png

WolframLanguageInRussian) rAdvancedFunctionSetting_58.gif

The obvious inconvenience of working with such functions is that if you want to use a lot of arguments with default values, you can only use these values ​​if they are to the right of those arguments to which you attach some value. If you change some default value to a specific value, then you will have to explicitly specify all the values ​​that are to the left of it. Thus, this task is very sensitive to the order of the arguments, which means that variables with default values ​​should be placed in order of decreasing frequency of their use, if the frequency is about the same, then the expediency of setting such functions drops sharply.

Setting a function with options


In order to get rid of the problem associated with the use of variables that have default values, which was discussed above, functions that have so-called options are used. Most of the built-in functions of Mathematica have certain options.

To set options for a certain function, use the built-in functions OptionsPattern and OptionValue , the meaning of which is clear from the following example: we implement the same function as before (building a circle or circle with the default center at (0, 0) ), radius 1, red color), only all default values ​​are now defined by options:

WolframLanguageInRussian) rAdvancedFunctionSetting_59.png

WolframLanguageInRussian) rAdvancedFunctionSetting_60.png

WolframLanguageInRussian) rAdvancedFunctionSetting_61.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_62.png

WolframLanguageInRussian) rAdvancedFunctionSetting_63.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_64.png

WolframLanguageInRussian) rAdvancedFunctionSetting_65.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_66.png

WolframLanguageInRussian) rAdvancedFunctionSetting_67.gif

Setting a function with conditions imposed on arguments


In order to impose a constraint on a function argument, the Condition function (which has a short form /; ) is used.

Let's say we create a function that would be calculated only if its argument is greater than 2:

WolframLanguageInRussian) rAdvancedFunctionSetting_68.png

WolframLanguageInRussian) rAdvancedFunctionSetting_69.png

WolframLanguageInRussian) rAdvancedFunctionSetting_70.gif

Or a function that would be calculated if its argument is a list consisting of no more than 4 elements:

WolframLanguageInRussian) rAdvancedFunctionSetting_71.png

WolframLanguageInRussian) rAdvancedFunctionSetting_72.png

WolframLanguageInRussian) rAdvancedFunctionSetting_73.gif

In the example above, a template was also used with the head of the expression List ( x_List ), which specifies any list.

This kind of patterns can be very different:

WolframLanguageInRussian) rAdvancedFunctionSetting_74.png

WolframLanguageInRussian) rAdvancedFunctionSetting_75.png

WolframLanguageInRussian) rAdvancedFunctionSetting_76.gif

You can also use all sorts of test functions that check whether an expression has a certain property. This is done using the PatternTest function (which has a short form ? ), The standard syntax of which is as follows:

WolframLanguageInRussian) rAdvancedFunctionSetting_77.gif

All tests (as can be seen from the code below, there are 128 of them) in Mathematica are functions that end up, as a rule, with a capital Q :

WolframLanguageInRussian) rAdvancedFunctionSetting_78.png

WolframLanguageInRussian) rAdvancedFunctionSetting_79.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_80.png
128

Let's say it’s just so easy to create a function that will work only with prime numbers:

WolframLanguageInRussian) rAdvancedFunctionSetting_82.png

WolframLanguageInRussian) rAdvancedFunctionSetting_83.png

WolframLanguageInRussian) rAdvancedFunctionSetting_84.gif

Alternative templates


In some cases, the function must work with an expression if it satisfies one of the specified patterns. You can implement a similar construct using the Alternatives function (which has a short form | ).

Below is a function whose argument can be either a list or an integer greater than 4:

WolframLanguageInRussian) rAdvancedFunctionSetting_85.png

WolframLanguageInRussian) rAdvancedFunctionSetting_86.png

WolframLanguageInRussian) rAdvancedFunctionSetting_87.gif

Multiple function assignment (function overload)


In Mathematica , you have the opportunity to define the function definitions that will be used in different situations. This can be done by applying patterns with the head parts of expressions, as well as using constraints imposed on variables.

In the example below, a function is created that processes input data in different ways depending on their type:

WolframLanguageInRussian) rAdvancedFunctionSetting_88.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_89.png

WolframLanguageInRussian) rAdvancedFunctionSetting_90.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_91.png

WolframLanguageInRussian) rAdvancedFunctionSetting_92.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_93.png

WolframLanguageInRussian) rAdvancedFunctionSetting_94.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_95.png

WolframLanguageInRussian) rAdvancedFunctionSetting_96.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_97.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_98.gif

Example: composition of previously described methods for defining functions and its arguments


WolframLanguageInRussian) rAdvancedFunctionSetting_99.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_100.png

WolframLanguageInRussian) rAdvancedFunctionSetting_101.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_102.png

WolframLanguageInRussian) rAdvancedFunctionSetting_103.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_104.png

WolframLanguageInRussian) rAdvancedFunctionSetting_105.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_106.png

WolframLanguageInRussian) rAdvancedFunctionSetting_107.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_108.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_109.gif

Programming in Mathematica in Russian


Now that we know how functions are defined, we can set ourselves the following task: to make it possible to program in Mathematica in Russian in compliance with the main paradigms: the built-in functions are capitalized and their names correspond exactly to the action they perform .

For this, you need to make Russian analogues of built-in functions.

Let's start with the Plot function, in Russian it will correspond to the Graph function. The syntax of the Plot function is:

WolframLanguageInRussian) rAdvancedFunctionSetting_110.png

Her Russian equivalent will be completely analogous.

To begin with, we will find out the list of all the options of the Plot function and their default values, this can be done using the Options function:

WolframLanguageInRussian) rAdvancedFunctionSetting_111.png

WolframLanguageInRussian) rAdvancedFunctionSetting_112.gif

Now we will create this list in Russian (some options are omitted here, which, by experience, are used extremely rarely when plotting):

WolframLanguageInRussian) rAdvancedFunctionSetting_113.png

Now let's set the connection of Russian names with embedded analogs:

WolframLanguageInRussian) rAdvancedFunctionSetting_114.png

Now we can set the Russian equivalent of the Plot function:

WolframLanguageInRussian) rAdvancedFunctionSetting_115.png

We also need some standard notation for the thickness and color of the lines:

WolframLanguageInRussian) rAdvancedFunctionSetting_116.gif

Similarly, we define the Solve function — the Russian analogue of the Solve function; Absolute Point Size — AbsolutePointSize ; Point — Point ; Line — Line :

WolframLanguageInRussian) rAdvancedFunctionSetting_117.gif

Let's try to create some schedule:

WolframLanguageInRussian) rAdvancedFunctionSetting_118.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_119.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_120.gif

WolframLanguageInRussian) rAdvancedFunctionSetting_121.gif

Thus, in Mathematica it is possible to create a package that will allow programming in ordinary Russian, just as it was done in the USSR, say in the ALMIR-65 language.

If any of you have a desire to develop this idea, then it is quite possible to make Mathematica work in Russian.

Source: https://habr.com/ru/post/256067/


All Articles