This is from C语言中文网,just for my self-learning and sharing with someone.
C语言pow()函数:
pow() 函数用来求 x 的 y 次幂(次方),其原型为: double pow(double x, double y);
pow()用来计算以x 为底的 y 次方值,然后将结果返回。设返回值为 ret,则 ret = xy。
可能导致错误的情况:
- 如果底数 x 为负数并且指数 y 不是整数,将会导致 domain error 错误。
- 如果底数 x 和指数 y 都是 0,可能会导致 domain error 错误,也可能没有;这跟库的实现有关。
- 如果底数 x 是 0,指数 y 是负数,可能会导致 domain error 或 pole error 错误,也可能没有;这跟库的实现有关。
- 如果返回值 ret 太大或者太小,将会导致 range error 错误。
错误代码:
- 如果发生 domain error 错误,那么全局变量 errno 将被设置为 EDOM;
- 如果发生 pole error 或 range error 错误,那么全局变量 errno 将被设置为 ERANGE。
- 注意,使用 GCC 编译时请加入-lm。
C Functions
mxCreateDoubleMatrix | 2-D, double-precision, floating-point array |
mxCreateDoubleScalar | Scalar, double-precision array initialized to specified value |
mxCreateNumericMatrix | 2-D numeric matrix |
mxCreateNumericArray | N-D numeric array |
mxCreateString | 1-N array initialized to specified string |
mxCreateCharMatrixFromStrings | 2-D string array initialized to specified value |
mxCreateCharArray | N-D string array |
mxCreateLogicalScalar | Scalar, logical array |
mxCreateLogicalMatrix | 2-D logical array |
mxCreateLogicalArray | N-D logical array |
mxCreateSparseLogicalMatrix | 2-D, sparse, logical array |
mxCreateSparse | 2-D sparse array |
mxCreateSparseLogicalMatrix | 2-D, sparse, logical array |
mxCreateStructMatrix | 2-D structure array |
mxCreateStructArray | N-D structure array |
mxCreateCellMatrix | 2-D cell array |
mxCreateCellArray | N-D cell array |
mxDestroyArray | Free dynamic memory allocated by MXCREATE* functions |
mxDuplicateArray | Make deep copy of array |
mxCalloc | Allocate dynamic memory for array, initialized to 0, using MATLAB memory manager |
mxMalloc | Allocate uninitialized dynamic memory using MATLAB memory manager |
mxRealloc | Reallocate dynamic memory using MATLAB memory manager |
mxFree | Free dynamic memory allocated by MXCALLOC, MXMALLOC, or MXREALLOC functions |
mxCreateNumericArray (C and Fortran)
C Syntax
#include "matrix.h" mxArray *mxCreateNumericArray(mwSize ndim, const mwSize *dims, mxClassID classid, mxComplexity ComplexFlag);
Fortran Syntax
mwPointer mxCreateNumericArray(ndim, dims, classid, ComplexFlag) mwSize ndim mwSize dims(ndim) integer*4 classid, ComplexFlag
Arguments
-
ndim
-
Number of dimensions. If you specify a value for ndim that is less than 2, mxCreateNumericArray automatically sets the number of dimensions to 2.
dims
-
Dimensions array. Each element in the dimensions array contains the size of the array in that dimension. For example, in C, setting dims[0] to 5 and dims[1] to 7 establishes a 5-by-7 mxArray. In Fortran, setting dims(1)to 5 and dims(2) to 7 establishes a 5-by-7 mxArray. In most cases, there are ndim elements in the dims array.
classid
-
Identifier for the class of the array, which determines the way the numerical data is represented in memory. For example, specifying mxINT16_CLASS in C causes each piece of numerical data in the mxArray to be represented as a 16-bit signed integer. In Fortran, use the function mxClassIDFromClassName to derive theclassid value from a MATLAB® class name. See the Description section for more information.
ComplexFlag
-
If the mxArray you are creating is to contain imaginary data, set ComplexFlag to mxCOMPLEX in C (1 in Fortran). Otherwise, set ComplexFlag to mxREAL in C (0 in Fortran).
Returns
Pointer to the created mxArray, if successful. If unsuccessful in a standalone (non-MEX-file) application, returnsNULL in C (0 in Fortran). If unsuccessful in a MEX-file, the MEX-file terminates and returns control to the MATLAB prompt. The function is unsuccessful when there is not enough free heap space to create the mxArray.
Description
Call mxCreateNumericArray to create an N-dimensional mxArray in which all data elements have the numeric data type specified by classid. After creating the mxArray, mxCreateNumericArray initializes all its real data elements to 0. If ComplexFlag equals mxCOMPLEX in C (1 in Fortran), mxCreateNumericArray also initializes all its imaginary data elements to 0. mxCreateNumericArray differs from mxCreateDoubleMatrix as follows:
-
All data elements in mxCreateDoubleMatrix are double-precision, floating-point numbers. The data elements in mxCreateNumericArray can be any numerical type, including different integer precisions.
-
mxCreateDoubleMatrix can create two-dimensional arrays only; mxCreateNumericArray can create arrays of two or more dimensions.
mxCreateNumericArray allocates dynamic memory to store the created mxArray. When you finish with the created mxArray, call mxDestroyArray to deallocate its memory.
MATLAB automatically removes any trailing singleton dimensions specified in the dims argument. For example, ifndim equals 5 and dims equals [4 1 7 1 1], the resulting array has the dimensions 4-by-1-by-7.
The following table shows the C classid values and the Fortran data types that are equivalent to MATLAB classes.
MATLAB Class Name | C classid Value | Fortran Type |
---|---|---|
int8 | mxINT8_CLASS | BYTE |
uint8 | mxUINT8_CLASS | |
int16 | mxINT16_CLASS | INTEGER*2 |
uint16 | mxUINT16_CLASS | |
int32 | mxINT32_CLASS | INTEGER*4 |
uint32 | mxUINT32_CLASS | |
int64 | mxINT64_CLASS | INTEGER*8 |
uint64 | mxUINT64_CLASS | |
single | mxSINGLE_CLASS | REAL*4 |
double | mxDOUBLE_CLASS | REAL*8 |
single, with imaginary components | mxSINGLE_CLASS | COMPLEX*8 |
double, with imaginary components | mxDOUBLE_CLASS | COMPLEX*16 |
Examples
See the following examples in matlabroot/extern/examples/refbook.
See the following examples in matlabroot/extern/examples/mx.
mxGetPr (C and Fortran)
C Syntax
#include "matrix.h" double *mxGetPr(const mxArray *pm);
Returns
Pointer to the first element of the real data. Returns NULL in C (0 in Fortran) if there is no real data.
Description
Use mxGetPr on arrays of type double only. Use mxIsDouble to validate the mxArray type. For other mxArraytypes, use mxGetData.
Call mxGetPr to access the real data in the mxArray that pm points to. Once you have the starting address, you can access any other element in the mxArray.