"Fossies" - the Fresh Open Source Software Archive

Member "laspack/vector.c" (27 Mar 1995, 4940 Bytes) of package /linux/privat/old/laspack.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /****************************************************************************/
    2 /*                                 vector.c                                 */
    3 /****************************************************************************/
    4 /*                                                                          */
    5 /* type VECTOR                                                              */
    6 /*                                                                          */
    7 /* Copyright (C) 1992-1995 Tomas Skalicky. All rights reserved.             */
    8 /*                                                                          */
    9 /****************************************************************************/
   10 /*                                                                          */
   11 /*        ANY USE OF THIS CODE CONSTITUTES ACCEPTANCE OF THE TERMS          */
   12 /*              OF THE COPYRIGHT NOTICE (SEE FILE COPYRGHT.H)               */
   13 /*                                                                          */
   14 /****************************************************************************/
   15 
   16 #include <stddef.h>
   17 #include <stdlib.h>
   18 #include <string.h>
   19 
   20 #include "laspack/vector.h"
   21 #include "laspack/errhandl.h"
   22 #include "laspack/copyrght.h"
   23 
   24 void V_Constr(Vector *V, char *Name, size_t Dim, InstanceType Instance,
   25               Boolean OwnData)
   26 /* constructor of the type Vector */
   27 {
   28     V->Name = (char *)malloc((strlen(Name) + 1) * sizeof(char));
   29     if (V->Name != NULL)
   30         strcpy(V->Name, Name);
   31     else
   32         LASError(LASMemAllocErr, "V_Constr", Name, NULL, NULL);
   33     V->Dim = Dim;
   34     V->Instance = Instance;
   35     V->LockLevel = 0;
   36     V->Multipl = 1.0;
   37     V->OwnData = OwnData;
   38     if (OwnData) {
   39         if (LASResult() == LASOK) {
   40             V->Cmp = (Real *)malloc((Dim + 1) * sizeof(Real));
   41             if (V->Cmp == NULL) 
   42                 LASError(LASMemAllocErr, "V_Constr", Name, NULL, NULL);
   43         } else {
   44             V->Cmp = NULL;
   45         }
   46     }
   47 }
   48 
   49 void V_Destr(Vector *V)
   50 /* destructor of the type Vector */
   51 {
   52     if (V->Name != NULL)
   53         free(V->Name);
   54     if (V->OwnData) {
   55         if (V->Cmp != NULL) {
   56             free(V->Cmp);
   57             V->Cmp = NULL;
   58         }
   59     }
   60 }
   61 
   62 void V_SetName(Vector *V, char *Name)
   63 /* (re)set name of the vector V */
   64 {
   65     if (LASResult() == LASOK) {
   66         free(V->Name);
   67         V->Name = (char *)malloc((strlen(Name) + 1) * sizeof(char));
   68         if (V->Name != NULL)
   69             strcpy(V->Name, Name);
   70         else
   71             LASError(LASMemAllocErr, "V_SetName", Name, NULL, NULL);
   72     }
   73 }
   74 
   75 char *V_GetName(Vector *V)
   76 /* returns the name of the vector V */
   77 {
   78     if (LASResult() == LASOK)
   79         return(V->Name);
   80     else
   81         return("");
   82 }
   83 
   84 size_t V_GetDim(Vector *V)
   85 /* returns dimension of the vector V */
   86 {
   87     size_t Dim;
   88 
   89     if (LASResult() == LASOK)
   90         Dim = V->Dim;
   91     else
   92         Dim = 0;
   93     return(Dim);
   94 }
   95 
   96 void V_SetCmp(Vector *V, size_t Ind, Real Val)
   97 /* set a value of a vector component */
   98 {
   99     if (LASResult() == LASOK) {
  100         if (Ind > 0 && Ind <= V->Dim && V->Instance == Normal && V->OwnData == True) {
  101             V->Cmp[Ind] = Val;
  102         } else {
  103             LASError(LASRangeErr, "V_SetCmp", V->Name, NULL, NULL);
  104         }
  105     }
  106 }
  107 
  108 void V_SetAllCmp(Vector *V, Real Val)
  109 /* set all vector components equal Val */
  110 {
  111     size_t Dim, Ind;
  112     Real *VCmp;
  113 
  114     if (LASResult() == LASOK) {
  115         Dim = V->Dim;
  116         VCmp = V->Cmp;
  117         for(Ind = 1; Ind <= Dim; Ind++)
  118             VCmp[Ind] = Val;
  119         V->Multipl = 1.0;
  120     }
  121 }
  122 
  123 void V_SetRndCmp(Vector *V)
  124 /* set random components of the vector V */
  125 {
  126     size_t Dim, Ind;
  127     Real *VCmp;
  128 
  129     if (LASResult() == LASOK) {
  130         Dim = V_GetDim(V);
  131         VCmp = V->Cmp;
  132         for (Ind = 1; Ind <= Dim; Ind++) {
  133             VCmp[Ind] = (double)rand() / ((double)RAND_MAX + 1.0);
  134         }
  135         V->Multipl = 1.0;
  136     }
  137 }
  138 
  139 Real V_GetCmp(Vector *V, size_t Ind)
  140 /* returns the value of a vector component */
  141 {
  142     Real Val;
  143 
  144     if (LASResult() == LASOK) {
  145         if (Ind > 0 && Ind <= V->Dim) {
  146             Val = V->Cmp[Ind];
  147         } else {
  148             LASError(LASRangeErr, "V_GetCmp", V->Name, NULL, NULL);
  149             Val = 0.0;
  150         }
  151     } else {
  152         Val = 0.0;
  153     }
  154     return(Val);
  155 }
  156 
  157 void V_AddCmp(Vector *V, size_t Ind, Real Val)
  158 /* add a value to a vector component */
  159 {
  160     if (LASResult() == LASOK) {
  161         if (Ind > 0 && Ind <= V->Dim && V->Instance == Normal && V->OwnData == True) {
  162             V->Cmp[Ind] += Val;
  163         } else {
  164             LASError(LASRangeErr, "V_AddCmp", V->Name, NULL, NULL);
  165         }
  166     }
  167 }
  168 
  169 void V_Lock(Vector *V)
  170 /* lock the vector V */
  171 {
  172     if (V != NULL) 
  173         V->LockLevel++;
  174 }
  175 
  176 void V_Unlock(Vector *V)
  177 /* unlock the vector V */
  178 {
  179     if (V != NULL) {
  180         V->LockLevel--;
  181         if (V->Instance == Tempor && V->LockLevel <= 0) {
  182             V_Destr(V); 
  183         free(V);
  184     }
  185     }
  186 }