-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvecteurs.c
113 lines (104 loc) · 2.67 KB
/
vecteurs.c
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
#include <Python.h>
static PyObject* sumv(PyObject* self, PyObject* args)
{
int numElems1,numElems2;
int i;
long valeur=0;
PyObject* list1=NULL;
PyObject* list2=NULL;
PyObject* tuple;
PyObject* liste;
if(PyArg_ParseTuple(args, "O!O!",&PyTuple_Type,&list1,&PyTuple_Type,&list2))
{
numElems1=PyTuple_Size(list1);
numElems2=PyTuple_Size(list2);
if(numElems1!=numElems1)
{
printf("Error.The dimension of the vectors arguments are not the same\n");
}
tuple = PyTuple_New(numElems1);
for(i=0;i<numElems1;i++)
{
valeur=PyInt_AsLong(PyTuple_GetItem(list1,i))+PyInt_AsLong(PyTuple_GetItem(list2,i));
PyTuple_SetItem(tuple, i, PyInt_FromLong( valeur));
}
return tuple;
}
else if(PyArg_ParseTuple(args, "O!O!",&PyList_Type,&list1,&PyList_Type,&list2))
{
numElems1=PyList_Size(list1);
numElems2=PyList_Size(list2);
if(numElems1!=numElems1)
{
printf("Error.The dimension of the vectors arguments are not the same\n");
}
liste = PyList_New(0);
for(i=0;i<numElems1;i++)
{
valeur=PyInt_AsLong(PyList_GetItem(list1,i))+PyInt_AsLong(PyList_GetItem(list2,i));
PyList_Append(liste, PyInt_FromLong( valeur));
}
return liste;
}
else
{
printf("Type error: the arguments must be tuples or lists\n");
return NULL;
}
}
static PyObject* diffv(PyObject* self, PyObject* args)
{
int numElems1,numElems2;
int i;
long valeur=0;
PyObject* list1=NULL;
PyObject* list2=NULL;
PyObject* tuple;
PyObject* liste;
if(PyArg_ParseTuple(args, "O!O!",&PyTuple_Type,&list1,&PyTuple_Type,&list2))
{
numElems1=PyTuple_Size(list1);
numElems2=PyTuple_Size(list2);
if(numElems1!=numElems1)
{
printf("Error.The dimension of the vectors arguments are not the same\n");
}
tuple = PyTuple_New(numElems1);
for(i=0;i<numElems1;i++)
{
valeur=PyInt_AsLong(PyTuple_GetItem(list1,i))-PyInt_AsLong(PyTuple_GetItem(list2,i));
PyTuple_SetItem(tuple, i, PyInt_FromLong( valeur));
}
return tuple;
}
else if(PyArg_ParseTuple(args, "O!O!",&PyList_Type,&list1,&PyList_Type,&list2))
{
numElems1=PyList_Size(list1);
numElems2=PyList_Size(list2);
if(numElems1!=numElems1)
{
printf("Error.The dimension of the vectors arguments are not the same\n");
}
liste = PyList_New(0);
for(i=0;i<numElems1;i++)
{
valeur=PyInt_AsLong(PyList_GetItem(list1,i))-PyInt_AsLong(PyList_GetItem(list2,i));
PyList_Append(liste, PyInt_FromLong( valeur));
}
return liste;
}
else
{
printf("Type error: the arguments must be tuples or lists\n");
return NULL;
}
}
static PyMethodDef fonctions[]={
{"sumv", sumv, METH_VARARGS,"sum of 2 lists or tuple"},
{"diffv", diffv, METH_VARARGS,"difference of 2 lists or tuple"},
{NULL, NULL,0,NULL}
};
void initvecteurs(void)
{
Py_InitModule("vecteurs", fonctions);
}