-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdif.c
More file actions
138 lines (124 loc) · 3.57 KB
/
Copy pathdif.c
File metadata and controls
138 lines (124 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <Python.h>
#include <math.h>
static PyObject *d_sin(PyObject *self, PyObject *args){
double x;
if (!PyArg_ParseTuple(args, "d", &x)){
return NULL;
}
return Py_BuildValue("d", cos(x));
}
static PyObject *d_cos(PyObject *self, PyObject *args){
double x;
if (!PyArg_ParseTuple(args, "d", &x)){
return NULL;
}
return Py_BuildValue("d", -sin(x));
}
static PyObject *d_ln(PyObject *self, PyObject *args){
double x;
if (!PyArg_ParseTuple(args, "d", &x)){
return NULL;
}
if (x <= 0) {
PyErr_SetString(PyExc_ValueError, "The argument must be strictly greater than 0!");
return NULL;
}
return Py_BuildValue("d", 1/x);
}
static PyObject *d_tan(PyObject *self, PyObject *args){
double x;
if (!PyArg_ParseTuple(args, "d", &x)){
return NULL;
}
if (cos(x) == 0) {
PyErr_SetString(PyExc_ValueError, "The argument must not be pi/2 + pi*n!");
return NULL;
}
return Py_BuildValue("d", 1/(pow(cos(x),2)));
}
static PyObject *d_ctan(PyObject *self, PyObject *args){
double x;
if (!PyArg_ParseTuple(args, "d", &x)){
return NULL;
}
if (sin(x) == 0) {
PyErr_SetString(PyExc_ValueError, "The argument must not be pi*n!");
return NULL;
}
return Py_BuildValue("d", -1/pow(sin(x),2));
}
static PyObject *d_arctan(PyObject *self, PyObject *args){
double x;
if (!PyArg_ParseTuple(args, "d", &x)){
return NULL;
}
return Py_BuildValue("d", 1/(1+pow(x,2)));
}
static PyObject *d_arcctan(PyObject *self, PyObject *args){
double x;
if (!PyArg_ParseTuple(args, "d", &x)){
return NULL;
}
return Py_BuildValue("d", -1/(1+pow(x,2)));
}
static PyObject *d_pow_a(PyObject *self, PyObject *args){
double x,a;
if (!PyArg_ParseTuple(args, "dd", &x,&a)){
return NULL;
}
if (a <= 0) {
PyErr_SetString(PyExc_ValueError, "The argument 'a' must be strictly greater than 0!");
return NULL;
}
return Py_BuildValue("d", pow(a,x)*log(a));
}
static PyObject *d_log(PyObject *self, PyObject *args){
double x,a;
if (!PyArg_ParseTuple(args, "dd", &x,&a)){
return NULL;
}
if (a <= 0 || a==1) {
PyErr_SetString(PyExc_ValueError, "The argument 'a' must be strictly greater than 0 and 'a' must not be equal 1!");
return NULL;
}
if (x <= 0) {
PyErr_SetString(PyExc_ValueError, "The argument 'x' must be strictly greater than 0!");
return NULL;
}
return Py_BuildValue("d", 1/(x*log(a)));
}
static PyObject *d_pow_x(PyObject *self, PyObject *args){
double x,a;
if (!PyArg_ParseTuple(args, "dd", &x,&a)){
return NULL;
}
return Py_BuildValue("d", a*pow(x,a-1));
}
static PyMethodDef def_methods[] = {
{"d_sin", d_sin, METH_VARARGS, "doc_sin"},
{"d_cos", d_cos, METH_VARARGS, "doc_cos"},
{"d_ln", d_ln, METH_VARARGS, "doc_ln"},
{"d_arctan", d_arctan, METH_VARARGS, "doc_arctan"},
{"d_arcctan", d_arcctan, METH_VARARGS, "doc_arcctan"},
{"d_tan", d_tan, METH_VARARGS, "doc_tan"},
{"d_ctan", d_ctan, METH_VARARGS, "doc_ctan"},
{"d_pow_a", d_pow_a, METH_VARARGS, "doc_pow_a"},
{"d_log", d_log, METH_VARARGS, "doc_log"},
{"d_pow_x", d_pow_x, METH_VARARGS, "doc_pow_x"},
{NULL, NULL, 0, NULL}
};
static PyModuleDef def_module = {
PyModuleDef_HEAD_INIT,
"dif",
"doc_different",
-1,
def_methods,
NULL,NULL,NULL
};
PyMODINIT_FUNC PyInit_dif(void)
{
PyObject* m;
m = PyModule_Create(&def_module);
if (m == NULL)
return NULL;
return m;}