// This file is part of the Herezh++ application.
//
// The finite element software Herezh++ is dedicated to the field
// of mechanics for large transformations of solid structures.
// It is developed by Gérard Rio (APP: IDDN.FR.010.0106078.000.R.P.2006.035.20600)
// INSTITUT DE RECHERCHE DUPUY DE LÔME (IRDL) <https://www.irdl.fr/>.
//
// Herezh++ is distributed under GPL 3 license ou ultérieure.
//
// Copyright (C) 1997-2022 Université Bretagne Sud (France)
// AUTHOR : Gérard Rio
// E-MAIL  : gerardrio56@free.fr
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// For more information, please consult: <https://herezh.irdl.fr/>.

#include "Suite_geometrique.h"

#include "MathUtil.h"


// affichage de la courbe
void Suite_geometrique::Affiche() const 
{ cout << "\n suite geometrique de terme U_0= " << U_0 
       << " et de raison p= " << p << " ";	
};

// interactif écran-clavier pour saisir les paramètres d'une suite
// si amplitude != 0 alors il faut n non nulle et:
// on a Somme_suite(n) = amplitude, ce qui permet pour les suites de réduire le nombre de paramètre à lire
void Suite_geometrique::Def_suite(double amplitude, int n)
{ if (amplitude == 0.)
   { cout << "\n valeur du terme initial U_0 de la suite ";
     U_0=lect_double();
     cout << "\n valeur de la raison de la progression p= ";
     p=lect_double();
   }
  else
  	{ if (n < 0)
  		{ cout << "\n erreur pour la definition d'une suite geometrique, n est negatif ! "
  		       << "\n Suite_geometrique::Def_suite(double amplitude, int n) ";
  		  Sortie(1);
  		};
      cout << "\n valeur de la raison de la progression p= ";
      p=lect_double();
      if (n == 0)
       // on a directement somme = U_0
       	{ U_0 = amplitude;}
      else
       { if (Abs(p-1) < ConstMath::trespetit)
  	       // cas d'une suite equidistante
  	       {U_0 = amplitude / n;}
  	      else
  	       { U_0 = amplitude * (1.-p) /(1.-PUISSn(p,n+1));};
       };
  	}; 
};


// ramène la valeur d'un élément n de la suite
double Suite_geometrique::U_n(int n ) 
  { return (U_0*PUISSn(p,n));
  };
  
// ramène la somme de la suite de 0 à n
// de manière arbitraire pour n=-1 ==> 0
// pour n < -1 --> erreur
double Suite_geometrique::Somme_Suite(int n) 
  { double ret = 0.;
    if (n > 0)
     { if (Abs(p-1) < ConstMath::trespetit)
  	     {// cas d'une suite equidistante
  	      ret = (n+1)*U_0;
  	      }
  	   else
  	     { ret = U_0 * (1.-PUISSn(p,n+1))/(1.-p);};
     }
    else if (n==0) {ret = U_0;}
    else if (n==-1) { ret = 0.;}
    else if (n<-1)
    	{ cout << "\n erreur n est inf a -1 "
    	       << "\n Suite_geometrique::Somme_Suite(int n)";
    	  Sortie(1);
    	};
	return ret;	
  };