美文网首页
Mathematica(1)

Mathematica(1)

作者: zzzyxxx | 来源:发表于2019-10-15 22:14 被阅读0次

参考:An Introduction to Programming with Mathematica

1 An introduction to Mathematica

The syntax of inputs

Functions

  • N——数值
  • Factor——因式分解


  • Sqrt——square roots
  • Log——logarithms
  • Det——determinant of a matrix
  • Conjugate——conjugate of a complex number


  • Plot——画图
  • Integrate——积分


  • Intersection——交集
  • Length——the number of elements in any expression
  • Append——Appending z to w+xy is equivalent to adding z as an argument to the Plus function.


  • square brackets [ and ] are used to enclose the arguments to functions
  • curly braces { and } are used to indicate a list or range of values (used to represent vectors, matrices and tensors)


  • When you end an expression with a semicolon (;), Mathematica computes its value but does not display it.


  • Finally, you can enter a comment – some words that are not evaluated – by entering the words between (* and *).


2 The Mathematica language

2.1 Expressions

Internal forms of expressions

  • The Head function can be used to identify types of objects. For numbers, it will report whether the number is an integer, a rational number, a real number, or a complex number.



  • In fact, every Mathematica expression has a Head that gives some information about that type of expression.

2.2 Definitions

Defining variables and functions

  • define a constant a to have a certain numeric value.



    To check what definitions are associated with a, use ?a.


  • To define a rule for a function f, enclose its arguments in square brackets and use x_ to indicate the variable that will be substituted for x on the right-hand side.



    You can evaluate f at different values by replacing x with the value you wish to use. These values can be numeric, exact, or symbolic.



    We clear the symbols that are no longer needed.

Immediate vs. delayed assignments

  • An immediate assignment is written Set[lhs,rhs] or, more commonly:
    lhs = rhs


  • A delayed assignment is set up with the SetDelayed function and is written Set:Delayed[lhs,rhs] or, in its standard input form:
    lhs := rhs



    Notice that the delayed assignment does not return a value when the assignment is made. In fact, the right-hand side will not be evaluated until the function rand2 is called.

Piecewise-defined functions


Functions with multiple definitions

2.3 Predicates and Boolean operations

Predicates

A predicate is a function that returns a value of true or false depending upon whether its argument passes a test.
For example, the predicate PrimeQ tests for the primality of its argument.


Other predicates are available for testing numbers to see whether they are even, odd, integral, and so on.

Relational and logical operators

  • The Boolean operation “and” is represented in Mathematica by And, with shorthand notation && or .
  • The logical “or” operator, represented by Or and with shorthand notation || (or ),
    is true when either of its arguments is true.
  • Mathematica also contains an operator for the exclusive or, Xor.


3 Lists

3.1 Introduction

  • sort any set of data


  • extract elements of a dataset based on some criteria. Here we select those
    numbers from a list that are greater than 0.


  • create list


  • Table function is a list that specifies the iterator variable and the values that it should range over.
  • Plot functions use lists to specify the range over which a variable
    should be evaluated.
  • lists are stored in the functional form using the List function with some
    arbitrary number of arguments.

    The arguments of the List function (the list elements) can be any type of expression,
    including numbers, symbols, functions, character strings, and even other lists.

3.2 Creating and measuring lists List

construction

Range[imin,imax, di] generates a list of ordered numbers starting from imin and going up to, but not exceeding, imax in increments of di.


If di is not specified, a value of one is used.

If neither imin nor di is specified, then both are given the value of 1.

It is not necessary for imin, imax, or di to be integers.
  • Table[expr,{i,imin,imax,di}] generates a list by evaluating expr a number of times.





Measuring lists

  • Length function tells us how many elements are in the list.


    In a nested list, each inner list is an element of the outer list. Therefore, the Length of a nested list indicates the number of inner lists, and not their sizes.
  • Dimensions function indicates that there are two inner lists, that each inner list contains three lists, and that the innermost lists each have two elements. MatrixForm may help to see the structure better.

    The number of dimensions of a (possibly nested) list, is given by ArrayDepth.

    This is identical to the number of levels in that expression, as displayed by TreeForm.

3.3 Manipulating lists

Testing a list

The locations of specific elements in a list can be determined using the Position function.



Extracting elements

  • Elements can easily be extracted from a specific location in a list.




  • For multi-dimensional lists





  • If you only specify one argument, the second is assumed to be All.


  • extract elements from specific locations in a list





  • discard elements from a list, keeping the rest


  • remove elements at specific locations


  • Certain extractions


Rearranging lists

  • canonical orderings: numbers are ordered by numerical value, with complex numbers first ordered by real part and then by absolute value of the imaginary part; symbols and strings are ordered alphabetically, powers and products are ordered in a manner corresponding to the terms in a polynomial; expressions are ordered depth-first with shorter expressions coming first.


  • When applied to a nested list, Sort will use the first element of each nested list to determine the order.


  • The order of the elements in a list can be reversed.


相关文章

网友评论

      本文标题:Mathematica(1)

      本文链接:https://www.haomeiwen.com/subject/ynmiuctx.html