Accueil > > > CALCUL DE SPIN
CALCUL DE SPIN
Information sur la source
Description
Ce code calcul les matrices des opérateurs de spin (Sx, Sy, Sz, et S^2) à partir du nombre spin s.
Ce petit programme va je pense bien servir pour les étudiants en mécanique quantique.
Affiche le résultat et l'enregistre dans un fichier HTML au cas ou ;-)
Source
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
- typedef float complex [2];
-
- /* r = a + b */
- void complex_add (complex a, complex b, complex *r)
- {
- complex buffer = { a [0] + b [0], a [1] + b [1]};
-
- *r [0] = buffer [0]; *r [1] = buffer [1];
- }
-
- /* r = a - b */
- void complex_dif (complex a, complex b, complex *r)
- {
- complex buffer = { a [0] - b [0], a [1] - b [1]};
-
- *r [0] = buffer [0]; *r [1] = buffer [1];
- }
-
- /* r = a * b */
- void complex_mul (complex a, complex b, complex *r)
- {
- complex buffer = { a [0] * b [0] - a [1] * b [1], a [0] * b [1] + a [1] * b [0]};
-
- *r [0] = buffer [0]; *r [1] = buffer [1];
- }
-
- /* r = sqrt (a) et a REEL !!! */
- void complex_sqrt (float a, complex *r)
- {
- if (a < 0)
- {
- *r [0] = 0.0f; *r [1] = (float)sqrt (a);
- }
- else
- {
- *r [0] = (float)sqrt (a); *r [1] = 0.0f;
- }
- }
-
- /* Structure pour caractériser les informations d'un spin */
- typedef struct TSpin_
- {
- float s;
- float *m;
- complex **mat_sx, **mat_sy, **mat_sz, **mat_ss;
- } TSpin;
-
-
- /* Pour libérer une strucure TSpin */
- void detruire_spin (TSpin *data)
- {
- free ((void*) (data)->m);
- free ((void*) (data)->mat_sx);
- free ((void*) (data)->mat_sy);
- free ((void*) (data)->mat_sz);
- free ((void*) (data)->mat_ss);
- }
-
- /* Pour créer une matrice de complex */
- complex ** __get_matrice (int t)
- {
- int i, j;
- complex **buffer;
-
- buffer = (complex**) malloc (sizeof(complex*)*t);
-
- for (i = 0; i < t; i ++)
- {
- buffer [i] = (complex*) malloc (sizeof(complex)*t);
-
- for (j = 0; j < t; j ++)
- {
- buffer [i] [j] [0] = 0.0f;
- buffer [i] [j] [1] = 0.0f;
- }
- }
-
-
- return buffer;
- }
-
- /* Pour créer une structure TSpin */
- void alloue_spin (TSpin *data, float s)
- {
- int t = (int)2*s+1;
-
- data->s = s;
- data->m = (float*) malloc (sizeof(float)*t);
-
- data->mat_sx = __get_matrice (t);
- data->mat_sy = __get_matrice (t);
- data->mat_sz = __get_matrice (t);
- data->mat_ss = __get_matrice (t);
- }
-
- /* Pour afficher un nombre complex */
- void __affiche_complex (complex a)
- {
- if (a [0] == 0.0f && a [1] != 0.0f)
- printf ("%01f i", a [1]);
- else if (a [1] == 0.0f)
- printf ("%02f", a [0]);
- else
- printf ("%02f + %02f i", a [0], a [1]);
- }
-
- /* Pour afficher une matrice de complex */
- void __affiche_matrice (complex **data, int t)
- {
- int i, j;
-
- for (i = 0; i < t; i ++)
- {
- for (j = 0; j < t; j ++)
- { __affiche_complex (data [i] [j]); printf ("\t");}
-
- printf ("\n");
- }
- }
-
- /* Pour afficher une structure spin */
- void affiche_spin (TSpin data)
- {
- int i, t = (int)2*data.s+1;
-
- printf ("Spin %02f :\n\n", data.s);
-
- printf ("Base m = ");
-
- for (i = 0; i < t; i ++)
- printf ("|%02f> ", data.m [i]);
-
- printf ("\n\nMatrice S^2 :\n\n");
- __affiche_matrice (data.mat_ss, t);
-
- printf ("\n\nMatrice Sx :\n\n");
- __affiche_matrice (data.mat_sx, t);
-
- printf ("\n\nMatrice Sy :\n\n");
- __affiche_matrice (data.mat_sy, t);
-
- printf ("\n\nMatrice Sz :\n\n");
- __affiche_matrice (data.mat_sz, t);
-
- printf ("\n\n");
- }
-
- /* Pour écrire un nombre complex dans un fichier */
- void ecrire_complex (complex a, FILE *fichier)
- {
- if (a [0] == 0.0f && a [1] != 0.0f)
- fprintf (fichier, "<font color=red><b>%01f i</b></font>", a [1]);
- else if (a [1] == 0.0f)
- if (a [0] == 0)
- fprintf (fichier, "%02f", a [0]);
- else
- fprintf (fichier, "<font color=red><b>%02f</b></font>", a [0]);
- else
- fprintf (fichier, "<font color=red><b>%02f + %02f i</b></font>", a [0], a [1]);
- }
-
- /* Pour écrire une matrice dans un fichier */
- void ecrire_matrice (complex **data, int t, FILE *fichier)
- {
- int i, j;
-
- fprintf (fichier, "<br><br><table border=1 width=90% align=center noWrap>");
- for (i = 0; i < t; i ++)
- {
- fprintf (fichier, "<tr>");
- for (j = 0; j < t; j ++)
- {
- fprintf (fichier, "<td noWrap>");
- ecrire_complex (data [i] [j], fichier);
- fprintf (fichier, "</td>");
- }
-
- fprintf (fichier, "</tr>");
- }
- fprintf (fichier, "</table><br><br>");
- }
-
-
- /* Pour écrire dans le fichier file_name une structure spin */
- void sauver_spin (TSpin data, char *file_name)
- {
- FILE *fichier;
- int i, t = (int)2*data.s+1;
-
- fichier = fopen (file_name, "w");
-
- fprintf (fichier, "<title>Etude de spin : generee par Decaux Thomas</title>");
-
- fprintf (fichier, "<br><br><h2>Etude Spin %02f :</h2>", data.s);
-
- fprintf (fichier, "Base m = ");
-
- for (i = 0; i < t; i ++)
- fprintf (fichier, "<b>|%02f> </b>", data.m [i]);
-
- fprintf (fichier, "<br><br>Matrice S^2 :\n\n");
- ecrire_matrice (data.mat_ss, t, fichier);
-
- fprintf (fichier, "\n\nMatrice Sx :\n\n");
- ecrire_matrice (data.mat_sx, t, fichier);
-
- fprintf (fichier, "\n\nMatrice Sy :\n\n");
- ecrire_matrice (data.mat_sy, t, fichier);
-
- fprintf (fichier, "\n\nMatrice Sz :\n\n");
- ecrire_matrice (data.mat_sz, t, fichier);
-
- fclose (fichier);
- }
-
- /* Pour calculer les informations d'un spin */
- void calcul_spin (TSpin *spin)
- {
- int i, j, t = (int)2*(spin->s) + 1;
- complex **sp, **sm, demi = {0.5, 0}, ii = {2, 2};
-
- // Vecteurs de base
- spin->m [0] = -spin->s;
-
- for (i = 1; i < t; i ++)
- spin->m [i] = spin->m[i-1] + 1;
-
- // Calcul de S²
- for (i = 0; i < t; i ++)
- spin->mat_ss [i][i][0] = spin->s*(spin->s + 1);
-
- // Calcul de Sz
- for (i = 0; i < t; i ++)
- spin->mat_sz [i][i][0] = spin->m [i];
-
- // Calcul de S+
- sp = __get_matrice (t);
- for (i = 0; i < t-1; i ++)
- complex_sqrt (spin->s*(spin->s+1) - spin->m [i] * (1+spin->m [i]), &sp [i][i+1]);
-
- // Calcul de S-
- sm = __get_matrice (t);
- for (i = t-1; i > 0; i --)
- complex_sqrt (spin->s*(spin->s+1) - spin->m [i] * (1-spin->m [i]), &sp [i][i-1]);
-
- // Calcul de Sx = (S+ + S-) / 2
- for (i = 0; i < t; i ++)
- for (j = 0; j < t; j ++)
- {
- complex_add (sp [i][j], sm [i] [j], &spin->mat_sx [i] [j]);
- complex_mul (spin->mat_sx [i] [j], demi, &spin->mat_sx [i] [j]);
- }
-
- // Calcul de Sy = (S+ - S-) / 2*i
- for (i = 0; i < t; i ++)
- for (j = 0; j < t; j ++)
- {
- complex_dif (sp [i][j], sm [i] [j], &spin->mat_sy [i] [j]);
- complex_mul (spin->mat_sy [i] [j], demi, &spin->mat_sy [i] [j]);
- complex_mul (spin->mat_sy [i] [j], ii, &spin->mat_sy [i] [j]);
- }
-
- free ((void*) sp);
- free ((void*) sm);
- }
-
- int main (int argc, char *argv [])
- {
- TSpin spin;
- float jaja;
-
- if (argc == 2)
- jaja = (float)atof (argv [1]);
- else
- jaja = 4;
-
- alloue_spin (&spin, jaja);
-
- calcul_spin (&spin);
-
- affiche_spin (spin);
-
- if (argc >= 3)
- sauver_spin (spin, argv [2]);
- sauver_spin (spin, "jaja.html");
-
- detruire_spin (&spin);
-
- getchar ();
-
- return 0;
- }
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef float complex [2];
/* r = a + b */
void complex_add (complex a, complex b, complex *r)
{
complex buffer = { a [0] + b [0], a [1] + b [1]};
*r [0] = buffer [0]; *r [1] = buffer [1];
}
/* r = a - b */
void complex_dif (complex a, complex b, complex *r)
{
complex buffer = { a [0] - b [0], a [1] - b [1]};
*r [0] = buffer [0]; *r [1] = buffer [1];
}
/* r = a * b */
void complex_mul (complex a, complex b, complex *r)
{
complex buffer = { a [0] * b [0] - a [1] * b [1], a [0] * b [1] + a [1] * b [0]};
*r [0] = buffer [0]; *r [1] = buffer [1];
}
/* r = sqrt (a) et a REEL !!! */
void complex_sqrt (float a, complex *r)
{
if (a < 0)
{
*r [0] = 0.0f; *r [1] = (float)sqrt (a);
}
else
{
*r [0] = (float)sqrt (a); *r [1] = 0.0f;
}
}
/* Structure pour caractériser les informations d'un spin */
typedef struct TSpin_
{
float s;
float *m;
complex **mat_sx, **mat_sy, **mat_sz, **mat_ss;
} TSpin;
/* Pour libérer une strucure TSpin */
void detruire_spin (TSpin *data)
{
free ((void*) (data)->m);
free ((void*) (data)->mat_sx);
free ((void*) (data)->mat_sy);
free ((void*) (data)->mat_sz);
free ((void*) (data)->mat_ss);
}
/* Pour créer une matrice de complex */
complex ** __get_matrice (int t)
{
int i, j;
complex **buffer;
buffer = (complex**) malloc (sizeof(complex*)*t);
for (i = 0; i < t; i ++)
{
buffer [i] = (complex*) malloc (sizeof(complex)*t);
for (j = 0; j < t; j ++)
{
buffer [i] [j] [0] = 0.0f;
buffer [i] [j] [1] = 0.0f;
}
}
return buffer;
}
/* Pour créer une structure TSpin */
void alloue_spin (TSpin *data, float s)
{
int t = (int)2*s+1;
data->s = s;
data->m = (float*) malloc (sizeof(float)*t);
data->mat_sx = __get_matrice (t);
data->mat_sy = __get_matrice (t);
data->mat_sz = __get_matrice (t);
data->mat_ss = __get_matrice (t);
}
/* Pour afficher un nombre complex */
void __affiche_complex (complex a)
{
if (a [0] == 0.0f && a [1] != 0.0f)
printf ("%01f i", a [1]);
else if (a [1] == 0.0f)
printf ("%02f", a [0]);
else
printf ("%02f + %02f i", a [0], a [1]);
}
/* Pour afficher une matrice de complex */
void __affiche_matrice (complex **data, int t)
{
int i, j;
for (i = 0; i < t; i ++)
{
for (j = 0; j < t; j ++)
{ __affiche_complex (data [i] [j]); printf ("\t");}
printf ("\n");
}
}
/* Pour afficher une structure spin */
void affiche_spin (TSpin data)
{
int i, t = (int)2*data.s+1;
printf ("Spin %02f :\n\n", data.s);
printf ("Base m = ");
for (i = 0; i < t; i ++)
printf ("|%02f> ", data.m [i]);
printf ("\n\nMatrice S^2 :\n\n");
__affiche_matrice (data.mat_ss, t);
printf ("\n\nMatrice Sx :\n\n");
__affiche_matrice (data.mat_sx, t);
printf ("\n\nMatrice Sy :\n\n");
__affiche_matrice (data.mat_sy, t);
printf ("\n\nMatrice Sz :\n\n");
__affiche_matrice (data.mat_sz, t);
printf ("\n\n");
}
/* Pour écrire un nombre complex dans un fichier */
void ecrire_complex (complex a, FILE *fichier)
{
if (a [0] == 0.0f && a [1] != 0.0f)
fprintf (fichier, "<font color=red><b>%01f i</b></font>", a [1]);
else if (a [1] == 0.0f)
if (a [0] == 0)
fprintf (fichier, "%02f", a [0]);
else
fprintf (fichier, "<font color=red><b>%02f</b></font>", a [0]);
else
fprintf (fichier, "<font color=red><b>%02f + %02f i</b></font>", a [0], a [1]);
}
/* Pour écrire une matrice dans un fichier */
void ecrire_matrice (complex **data, int t, FILE *fichier)
{
int i, j;
fprintf (fichier, "<br><br><table border=1 width=90% align=center noWrap>");
for (i = 0; i < t; i ++)
{
fprintf (fichier, "<tr>");
for (j = 0; j < t; j ++)
{
fprintf (fichier, "<td noWrap>");
ecrire_complex (data [i] [j], fichier);
fprintf (fichier, "</td>");
}
fprintf (fichier, "</tr>");
}
fprintf (fichier, "</table><br><br>");
}
/* Pour écrire dans le fichier file_name une structure spin */
void sauver_spin (TSpin data, char *file_name)
{
FILE *fichier;
int i, t = (int)2*data.s+1;
fichier = fopen (file_name, "w");
fprintf (fichier, "<title>Etude de spin : generee par Decaux Thomas</title>");
fprintf (fichier, "<br><br><h2>Etude Spin %02f :</h2>", data.s);
fprintf (fichier, "Base m = ");
for (i = 0; i < t; i ++)
fprintf (fichier, "<b>|%02f> </b>", data.m [i]);
fprintf (fichier, "<br><br>Matrice S^2 :\n\n");
ecrire_matrice (data.mat_ss, t, fichier);
fprintf (fichier, "\n\nMatrice Sx :\n\n");
ecrire_matrice (data.mat_sx, t, fichier);
fprintf (fichier, "\n\nMatrice Sy :\n\n");
ecrire_matrice (data.mat_sy, t, fichier);
fprintf (fichier, "\n\nMatrice Sz :\n\n");
ecrire_matrice (data.mat_sz, t, fichier);
fclose (fichier);
}
/* Pour calculer les informations d'un spin */
void calcul_spin (TSpin *spin)
{
int i, j, t = (int)2*(spin->s) + 1;
complex **sp, **sm, demi = {0.5, 0}, ii = {2, 2};
// Vecteurs de base
spin->m [0] = -spin->s;
for (i = 1; i < t; i ++)
spin->m [i] = spin->m[i-1] + 1;
// Calcul de S²
for (i = 0; i < t; i ++)
spin->mat_ss [i][i][0] = spin->s*(spin->s + 1);
// Calcul de Sz
for (i = 0; i < t; i ++)
spin->mat_sz [i][i][0] = spin->m [i];
// Calcul de S+
sp = __get_matrice (t);
for (i = 0; i < t-1; i ++)
complex_sqrt (spin->s*(spin->s+1) - spin->m [i] * (1+spin->m [i]), &sp [i][i+1]);
// Calcul de S-
sm = __get_matrice (t);
for (i = t-1; i > 0; i --)
complex_sqrt (spin->s*(spin->s+1) - spin->m [i] * (1-spin->m [i]), &sp [i][i-1]);
// Calcul de Sx = (S+ + S-) / 2
for (i = 0; i < t; i ++)
for (j = 0; j < t; j ++)
{
complex_add (sp [i][j], sm [i] [j], &spin->mat_sx [i] [j]);
complex_mul (spin->mat_sx [i] [j], demi, &spin->mat_sx [i] [j]);
}
// Calcul de Sy = (S+ - S-) / 2*i
for (i = 0; i < t; i ++)
for (j = 0; j < t; j ++)
{
complex_dif (sp [i][j], sm [i] [j], &spin->mat_sy [i] [j]);
complex_mul (spin->mat_sy [i] [j], demi, &spin->mat_sy [i] [j]);
complex_mul (spin->mat_sy [i] [j], ii, &spin->mat_sy [i] [j]);
}
free ((void*) sp);
free ((void*) sm);
}
int main (int argc, char *argv [])
{
TSpin spin;
float jaja;
if (argc == 2)
jaja = (float)atof (argv [1]);
else
jaja = 4;
alloue_spin (&spin, jaja);
calcul_spin (&spin);
affiche_spin (spin);
if (argc >= 3)
sauver_spin (spin, argv [2]);
sauver_spin (spin, "jaja.html");
detruire_spin (&spin);
getchar ();
return 0;
}
Conclusion
Mon site : http://www.iseninfo.com
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[TECHDAYS2012] OUI J'Y SERAI![TECHDAYS2012] OUI J'Y SERAI! par JeremyJeanson
Bonsoir, Certes, je l'annonce avec un peu de retard, mais je serai effectivement au Techdays demain. Comme l'an dernier, je participerai au programme ATE (Ask The Expert). Si vous avez des questions Workflow, WCF, AppFabric ou plus généralement .net, n'hé...
Cliquez pour lire la suite de l'article par JeremyJeanson TFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICESTFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICES par vfabing
Afin de s'assurer du bon fonctionnement des différentes synchronisations effectuées par les TFS Integration Tools, 2 rapports sont présents dès l'installation. Il suffit alors d'effectuer les manipulations suivantes pour pouvoir les visualiser : Loca...
Cliquez pour lire la suite de l'article par vfabing CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks
Forum
RE : ARBRE BINAIRERE : ARBRE BINAIRE par pacotheking
Cliquez pour lire la suite par pacotheking
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|