You are reading the article Working Of Md5() Functions In Php With Examples updated in December 2023 on the website Cattuongwedding.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Working Of Md5() Functions In Php With Examples
Introduction to PHP md5()The MD5() function of the PHP Programming Language will produce the hash of the string which is like encoding process. MD5() function works only on PHP 4, 5, 7 versions but for the other PHP version the hash encoder “md5()” may work or may not work mostly. Most of the times md5() function is not recommended to safely secure the passwords due to the function’s fast nature of encoding with the help of its inbuilt hashing algorithm. It accepts only two parameters. In those two only one is mandatory at all times.
Start Your Free Software Development Course
Syntax:
String md5 ($string, $getRawOutput)Explanation of Parameters in brief:
MD5() function of the PHP Programming Language takes two parameters at max. They are: $string parameter and $getRawOutput parameter.
$string: $string parameter will help us to expect the string to be hashed.
$getRawOutput: $getRawOutput parameter will help us to expect a Boolean value. For the TRUE result the function is going to return the HASH in raw binary format which is of the length 16.
Return type: The md5() function of PHP will return the hashed string ( it can either be in lowercase hex format character sequence which is of length 32 ( 32 character hexadecimal number )or for raw binary form which is of the length 16).
How do MD5() Functions work in PHP?MD5() function of the PHP Programming Language works for PHP 4, PHP 5 and PHP 7 versions up to now. Apart from these versions md5() function may not work mostly. It is a built-in function and by using the md5() function we initiate the HASHING algorithm inside of the PHP Programming Language. With the backend Hashing Algorithm, conversion of hashing of the specific numerical value/ string value/ any other will be done as needed. It is very helpful in the encoding process. MD5() function value will always be in 32 bit binary format unless second parameter is used inside of the md5() function. At that time md5() value will be 16 bit binary format.
Examples to Implement PHP md5()Below are the examples:
Example #1Code:
<?php $str1 = 'apples'; print "This is the value of HASH of apples :: "; $a1 = md5($str1); if (md5($str1) === '1f3870be274f6c49b3e31a0c6728957f') { echo "If the value of apples is :: 1f3870be274f6c49b3e31a0c6728957f then it will print :: "; } else{ }Output:
Example #2Code:
<?php $input_string1 = 'Pavan Kumar Sake'; echo '16 bit binary format :: '; $i1 = md5($input_string1,TRUE); echo $i1;Output:
Example #3Code:
<?php $k = 10; for($i=0;$i<=$k;$i++){ print "Hash code of $i :: "; print md5($i); } Example #4Code:
<?php $user1 = "Pavan Kumar Sake"; $pass1 = "pavansake123"; $user1_encode = md5($user1); $pass1_encode = md5($pass1); if (md5($user1)== "4c13476f5dd387106a2a629bf1a9a4a7"){ if(md5($pass1)== "20b424c60b8495fae92d450cd78eb56d"){ echo "Password is also correct so login will be successful"; } else{ echo "Incorrect Password is entered"; } } else{ echo "Incorrect Username is entered"; }Output:
ConclusionI hope you understood what is the definition of PHP md5() function with the syntax and its explanation, Info regarding the parameters in brief detail, Working of md5() function in PHP along with the various examples to understand the concept well.
Recommended ArticlesThis is a guide to PHP MD5(). Here we discuss the introduction, syntax, and working of MD5() in PHP along with different examples and code implementation. You can also go through our other related articles to learn more –
You're reading Working Of Md5() Functions In Php With Examples
How To Create Constants In Php With Examples?
Introduction to PHP Constants
PHP Constants are variables whose values, once defined, cannot be changed, and these constants are defined without a $ sign in the beginning. PHP Constants are created using define() function. This function takes two parameters first is the name, and the second is the value of the constant defined.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The name of the constant starts using letters or underscores and not with a number. It can start with a letter or underscore followed by letters, underscores or numbers. The name is case-sensitive and in uppercase. After a constant is defined, it cannot be undefined or redefined again. It remains the same throughout the script and cannot be changed as the variables do.
Syntax with ExplanationA constant is a name for a particular value. To define a constant, we have to use the define() function and to get the value of the constant; we just need to specify the name.
define(name, value, case-insensitive);where name is the name of the constant,
value is the value of the constant,
case-insensitive is either true or false, by default, it is false.
define('TEXT', 'Hello World!');A constant can also be defined using const construct.
<?php const MSG = "WELCOME"; echo MSG; How to Create Constants in PHP using Various Methods?To create constants, we have to use a simple define function, which takes two parameters, first the name of the constant second the value to be stored. The name is by default in uppercase. It does not start with a $.
Example #1Code:
<?php define("TEXT", "Hello World!"); echo TEXT;Output:
In this example, we will be using a const construct to define a constant named TEXT. We have used const followed by the name of the constant and then the value. It can be assigned a value using an assignment operator =.
Once we have defined the constant, to access the defined constant TEXT, we will echo the name with the constant keyword, as shown below.
Example #2Code:
<?php const TEXT = 'PHP PROGRAMMING!'; echo TEXT; echo constant("TEXT");Output:
Example #3In the below example, we are defining a TEXT constant with a value. Also, in the same program, we have defined a function Demo(). We have declared the TEXT constant outside the function Demo. Here we see that we can access the constant TEXT from within the function. This means once you define the constant, it is globally available in the script.
Code:
<?php define("TEXT", "Hello World!"); echo TEXT; function Demo() { echo TEXT; } Demo(); Rules and Regulations for PHP ConstantsThe following are the rules to define PHP constants.
should not start with a $.
should not start with a number.
should not start with an underscore.
start with a letter and follow by numbers.
start with a letter and follow by an underscore and numbers.
Let us look at the below statements.
<?php define("TEXT","PHP"); //valid define("TEXT1", "PHP"); define("1TEXT", "PHP"); //invalid define("1_TEXT", "PHP"); //invalid define("TEXT_1", "PHP"); define("__TEXT__", "PHP"); // valid but should be avoided Magic ConstantsIt starts with a double underscore
__LINE__
__FILE__
__FUNCTION__
__CLASS__
__METHOD__
1. __LINE__This gives the current line number.
Code:
<?php echo 'I am at Line number '. __LINE__;Output:
2.__FILE__This gives the filename along with the file path of the file. It can be used to include a file in a script.
Code:
<?php echo 'FILE NAME '. __FILE__; 3. __FUNCTION__This gives the name of the function in which it is declared. It is case-sensitive.
Code:
<?php function show() { echo 'In the function '.__FUNCTION__; } show();Output:
4. __METHOD__ , __CLASS__This gives the name of the method and the name of the class in which it is declared. In the below example, we have defined the MainClass and two methods within it, the show method and the test method. Inside the show method, we have printed the __CLASS__, which gives the class name and inside the test method, we have printed the __METHOD__, which gives the method name, test.
Code:
<?php class MainClass { function show() { } function test() { } } $obj = new MainClass;Output:
ConclusionThis article, it is explained about PHP constants and magic constants with examples. These examples help to create their own constants and use them in the script with the help of the given syntax. This article also explains the rules on how to create PHP Constants and then how to use them within the script with different methods.
Recommended ArticlesThis is a guide to PHP Constants. Here we discuss the introduction, syntax, and examples to create constants in PHP along with magic constants. You may also look at the following articles to learn more –
Learn Php Require_Once With Programming Examples
Introduction to PHP require_once
The require_once function of the PHP Programming Language helps in including a PHP file in another PHP file when that specific PHP file is included more than one time and we can consider it as an alternative for include function. If a PHP script file already included inside our required PHP file then that including will be ignored and again the new PHP file will be called just by ignoring the further inclusions. Suppose if chúng tôi is one of the PHP script file calling chúng tôi with require_once() function and it don’t fine chúng tôi chúng tôi stops executing and will cause a FATAL ERROR.
Start Your Free Software Development Course
Syntax:
require_once(' PHP file with the path '); How require_once works in PHP?The require_once function of the PHP language works only once even though if we include a specific PHP file or files inside of the main PHP script file. We can include a PHP script file/files as many times as we need but only once the specific PHP will be included and executed for the display of the output of the program. This require_once concept works only for PHP 4, PHP 5, PHP 7 versions and may be for PHP 7+ versions it will work. This first check whether the specific .PHP file is already included or not. If already included then the require_once function will leave including same .PHP file. If not, the specific .PHP file will be included. It is a very useful statement of the PHP Programming Language.
ExamplesBelow are the examples to implement the same:
Example #1Syntax of the Main PHP File:
</head> Engineers, Physics Nerds, Tech Enthusiasts to provide the better content.
Syntax of chúng tôi file:
<?php echo “This is by implementing the require_once statement of PHP..”; echo “Now you are in the chúng tôi files content!! because of require_once but calling twice don’t works with this”;
Syntax of chúng tôi file:
<?php echo “This is by implementing the require_once statement of PHP..”; echo “Now you are in the chúng tôi files content because of require_once but calling twice don’t works with this!!”;
<?php echo “This is by implementing the require_once statement of PHP..”; echo “Now you are in the chúng tôi files content because of require_once but calling twice don’t works with this!!”;
Output:
Example #2This is also another example of implementing this function. Here in the main original file (index.php) file, require_once() function is used 7 times to include chúng tôi In the chúng tôi file, only the chúng tôi file is included using the method few times. Then in the chúng tôi file, only chúng tôi file a few times and it is to include the content of the chúng tôi Here we didn’t mention any text in the chúng tôi files but at last in the chúng tôi file only sum code is implemented and that code will be executed and displayed along with the main original PHP file’s content/code. Footer code will be displayed inside of the horizontal lines. You can check the output for a better understanding.
Syntax of the main chúng tôi file:
<hrgt; Engineers, Physics Nerds, Tech Enthusiasts to provide better content.
Syntax of the chúng tôi file:
<?php require_once 'header1.php'; require_once 'header1.php'; require_once 'header1.php'; <?php require_once 'footer1.php'; require_once 'footer1.php'; require_once 'footer1.php'; require_once 'footer1.php';Syntax of the chúng tôi file:
<?php $i=10; $j=90; $k = $i + $j; echo "This content is included from the chúng tôi file - Code from chúng tôi file"; echo "This the sum of $i and $j values :: "; echo $k;
Output:
Advantages ConclusionI hope you learned what is the definition of the require_once function of the PHP Programming Language along with its syntax, How the PHP require_once function works along with some examples of which illustrate function, Advantages, etc. to understand this concept better and so easily.
Recommended ArticlesThis is a guide to PHP require_once. Here we discuss an introduction to PHP require_once, syntax, how does it work with examples to implement. You can also go through our other related articles to learn more –
Functions In C Programming With Examples: Recursive & Inline
What is a Function in C?
Function in C programming is a reusable block of code that makes a program easier to understand, test and can be easily modified without changing the calling program. Functions divide the code and modularize the program for better and effective results. In short, a larger program is divided into various subprograms which are called as functions
In this tutorial, you will learn-
Library Vs. User-defined FunctionsEvery ‘C’ program has at least one function which is the main function, but a program can have any number of functions. The main () function in C is a starting point of a program.
In ‘C’ programming, functions are divided into two types:
Library functions
User-defined functions
The difference between the library and user-defined functions in C is that we do not need to write a code for a library function. It is already present inside the header file which we always include at the beginning of a program. You just have to type the name of a function and use it along with the proper syntax. Printf, scanf are the examples of a library function.
Whereas, a user-defined function is a type of function in which we have to write a body of a function and call the function whenever we require the function to perform some operation in our program.
C programming functions are divided into three activities such as,
Function declaration
Function definition
Function call
Function DeclarationFunction declaration means writing a name of a program. It is a compulsory part for using functions in code. In a function declaration, we just specify the name of a function that we are going to use in our program like a variable declaration. We cannot use a function unless it is declared in a program. A function declaration is also called “Function prototype.”
The function declarations (called prototype) are usually done above the main () function and take the general form:
return_data_type function_name (data_type arguments);
The return_data_type: is the data type of the value function returned back to the calling statement.
The function_name: is followed by parentheses
Arguments names with their data type declarations optionally are placed inside the parentheses.
We consider the following program that shows how to declare a cube function to calculate the cube value of an integer variable
/*Function declaration*/ int add(int a,b); /*End of Function declaration*/ int main() {
Keep in mind that a function does not necessarily return a value. In this case, the keyword void is used.
For example, the output_message function declaration indicates that the function does not return a value: void output_message();
Function DefinitionFunction definition means just writing the body of a function. A body of a function consists of statements which are going to perform a specific task. A function body consists of a single or a block of statements. It is also a mandatory part of a function.
int add(int a,int b) { int c; c=a+b; return c; } Function callA function call means calling a function whenever it is required in a program. Whenever we call a function, it performs an operation for which it was designed. A function call is an optional part of a program.
result = add(4,5);Here, is th complete code:
int add(int a, int b); int main() { int a=10,b=20; int c=add(10,20); printf(“Addition:%dn”,c); getch(); } int add(int a,int b) { int c; c=a+b; return c; }
Output:
Addition:30 Function ArgumentsA function’s arguments are used to receive the necessary values by the function call. They are matched by position; the first argument is passed to the first parameter, the second to the second parameter and so on.
By default, the arguments are passed by value in which a copy of data is given to the called function. The actually passed variable will not change.
We consider the following program which demonstrates parameters passed by value:
int add (int x, int y); int main() { int a, b, result; a = 5; b = 10; result = add(a, b); printf("%d + %d = %dn", a, b, result); return 0;} int add (int x, int y) { x += y; return(x);}The program output is:
5 + 10 = 15Keep in mind that the values of a and b were passed to add function were not changed because only its value was passed into the parameter x.
Variable ScopeVariable scope means the visibility of variables within a code of the program.
In C, variables which are declared inside a function are local to that block of code and cannot be referred to outside the function. However, variables which are declared outside all functions are global and accessible from the entire program. Constants declared with a #define at the top of a program are accessible from the entire program. We consider the following program which prints the value of the global variable from both main and user defined function :
int global = 1348; void test(); int main() { printf(“from the main function : global =%d n”, global); test () ; return 0;}
void test (){ printf(“from user defined function : global =%d n”, global);}
Result:
from the main function : global =1348 from user defined function : global =1348We discuss the program details:
We declare an integer global variable with 1348 as initial value.
We declare and define a test() function which neither takes arguments nor returns a value. This function only prints the global variable value to demonstrate that the global variables can be accessed anywhere in the program.
We print the global variable within the main function.
We call the test function in order to print the global variable value.
In C, when arguments are passed to function parameters, the parameters act as local variables which will be destroyed when exiting the function.
When you use global variables, use them with caution because can lead to errors and they can change anywhere in a program. They should be initialized before using.
Static VariablesThe static variables have a local scope. However, they are not destroyed when exiting the function. Therefore, a static variable retains its value forever and can be accessed when the function is re-entered. A static variable is initialized when declared and needs the prefix static.
The following program uses a static variable:
void say_hi(); int main() { int i; for (i = 0; i < 5; i++) { say_hi();} return 0;} void say_hi() { static int calls_number = 1; printf(“Hi number %dn”, calls_number); calls_number ++; }
The program displays :
Hi number 1 Hi number 2 Hi number 3 Hi number 4 Hi number 5 Recursive FunctionsConsider the factorial of a number which is calculated as follow 6! =6* 5 * 4 * 3 * 2 * 1.
This calculation is done as repeatedly calculating fact * (fact -1) until fact equals 1.
A recursive function is a function which calls itself and includes an exit condition in order to finish the recursive calls. In the case of the factorial number calculation, the exit condition is fact equals to 1. Recursion works by “stacking” calls until the exiting condition is true.
For example:
int factorial(int number); int main() { int x = 6; printf(“The factorial of %d is %dn”, x, factorial(x)); return 0;} int factorial(int number) { if (number == 1) return (1); /* exiting condition */ else return (number * factorial(number – 1)); }
The program displays:
The factorial of 6 is 720Here, we discuss program details:
We declare our recursive factorial function which takes an integer parameter and returns the factorial of this parameter. This function will call itself and decrease the number until the exiting, or the base condition is reached. When the condition is true, the previously generated values will be multiplied by each other, and the final factorial value is returned.
We declare and initialize an integer variable with value”6″ and then print its factorial value by calling our factorial function.
Consider the following chart to more understand the recursive mechanism which consists of calling the function its self until the base case or stopping condition is reached, and after that, we collect the previous values:
Inline FunctionsFunction in C programming is used to store the most frequently used instructions. It is used for modularizing the program.
Whenever a function is called, the instruction pointer jumps to the function definition. After executing a function, instruction pointer falls back to the statement from where it jumped to the function definition.
In an inline function, a function call is directly replaced by an actual program code. It does not jump to any block because all the operations are performed inside the inline function.
Inline functions are mostly used for small computations. They are not suitable when large computing is involved.
An inline function is similar to the normal function except that keyword inline is place before the function name. Inline functions are created with the following syntax:
inline function_name () { }Let us write a program to implement an inline function.
inline int add(int a, int b) { return(a+b); } int main() { int c=add(10,20); printf("Addition:%dn",c); getch(); }Output:
Addition: 30Above program demonstrates the use of an inline function for addition of two numbers. As we can see, we have returned the addition on two numbers within the inline function only without writing any extra lines. During function call we have just passed values on which we have to perform addition.
Summary
A function is a mini-program or a subprogram.
Functions are used to modularize the program.
Library and user-defined are two types of functions.
A function consists of a declaration, function body, and a function call part.
Function declaration and body are mandatory.
A function call can be optional in a program.
C program has at least one function; it is the main function ().
Each function has a name, data type of return value or a void, parameters.
Each function must be defined and declared in your C program.
Keep in mind that ordinary variables in a C function are destroyed as soon as we exit the function call.
The arguments passed to a function will not be changed because they passed by value none by address.
The variable scope is referred to as the visibility of variables within a program
There are global and local variables in C programming
Complete Guide To Php Header() With Examples
Introduction to PHP header()
PHP header is an inbuilt function that is used to send a raw HTTP header to the client and it is mandatory that they actually manipulate the information which is sent to the client or browser before any original output can be sent. A raw request (like an HTTP request) is sent to the browser or the client before HTML, XML, JSON or any other output has been sent. HTTP headers contain required necessary information of the object which is sent in the message body more accurately on the request and its response.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax and ParameterBelow are the syntax and parameter:
Syntax
header_name() here is a mandatory field and sends the header string to be sent
Code:
<?php header('WWW-Authenticate: Negotiate'); echo ('header has been changed to WWW-Authenticate: Negotiate'); echo "n"; header('WWW-Authenticate: NTLM', false); echo ('header has been changed to WWW-Authenticate: NTLM');Output:
The header() here is used to send a raw HTTP header. This header hence must be called before any other output is been sent either by usual HTML tags, blank lines or from PHP. A few common mistakes are to read the code with include, access or any other require functions, having spaces or empty lines which are output before calling the header(). This problem also exists when we are using an individual PHP or an HTML file.
Return Values: header() function does not return any value. In header calls, there are 2 types: The first one starts with the string “HTTP/” (case insignificant) which is used to find out the HTTP status code to send.
Examples to Implement PHP header()Below are the examples:
Example #1Code:
<?php header("HTTP Error 404: Not Found"); echo ('Header been changed to HTTP Error 404: Not Found');Output:
Explanation: The second type is the Location header which sends the header back to a web browser and also returns back a REDIRECT status code to the browser until and unless status codes 201 or 3xx have been already sent.
Example #2Code:
<?php exit;Output:
Example #3Code:
<?php header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="file.pdf"'); readfile('oldfile.pdf');Explanation: In this example, we are prompting the user to save the generated PDF file being sent to them. For this purpose, we use the Content-Disposition header to give a required file name and to force the web browser to show the save dialog.
Example #4Code:
<?php header("Cache-Control: no-cache, hence should-revalidate"); echo('Displaying header information: Cache-Control: no-cache, hence should-revalidate' );Output:
Explanation: In this example, we are using certain proxies and clients to disable the caching process of PHP. This is because PHP often creates dynamic content that should not be cached by the web browser or any other proxy caches which come in between server and browser.
Sometimes it may happen that the pages will not be cached even if the above said lines and headers are not incorporated in the PHP code. This is because a lot of options are available which a user can set for his browser that actually changes its default set caching behavior. Hence by using the above-mentioned headers we will be able to override all the settings which may cause the output of PHP script to be cached.
There is also another configuration setting called the session.cache_limiter which generates the correct cache-related headers automatically when different sessions are being used.
Example #5Code:
<?php header("Cache-Control: no-cache"); header("Pragma: no-cache"); <!-- PHP program to display <?php print_r(headers_list());Output:
Explanation: The above-given example is used to prevent caching which sends the header information to override the browser setting so that it does not cache it. We are using the header() function multiple times in this example as only one header is allowed to send at one time. This prevents something called header injection attacks.
Example #6Code:
<?php header( "refresh:10;url=example.php" );Output:
Explanation: This example above is used to redirect the user and to inform him that he will be redirected.
Example #7Code:
<?php $headers = apache_request_headers(); if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == ftime($t1))) { echo(‘’); } else { header('Content-Length: '.filesize($t1)); header('Content-Type: image/png'); print file_get_contents($t1); }Output:
Explanation: In the above example, we are using PHP headers to cache an image being sent and hence bandwidth can be saved by doing this. First, we take the image and check if it is already cached, this by setting the cache to IS current. If it is not current then we are caching the same and sending the image in the output.
Advantages of using header function in PHP
PHP headers are very essential in redirecting the URI string also to display the appropriate message such as “404 Not Found Error”.
PHP headers can be used to tell the web browser what type the response is, and the content type.
The redirect script which will be used at the beginning helps in saving time of execution and bandwidth.
Conclusion Recommended ArticlesThis is a guide to PHP header(). Here we discuss an introduction to PHP header() along with appropriate Syntax, and top 7 examples to implement with proper codes and outputs. You can also go through our other related articles to learn more –
Working And Examples Of Numpy Norm
Introduction to NumPy norm
The error of a given model in machine learning and deep learning can be evaluated by using a function called norm which can be thought of as the length of the vector to map the vector to a given positive value, and the length of the vector can be calculated using three vector norms namely vector L1 norm, vector L2 norm and vector max norm where vector L1 norm represents the L1 norm of the vector which calculates the absolute vector values sum and vector L2 norm represents the L2 norm of the vector which calculates the squared vectored values sum and finds its square root and vector max norm calculates the vector’s maximum value. In this topic, we are going to learn about the NumPy norm.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
SyntaxThe syntax for NumPy norm in Python is as follows:
and the syntax for the same is as follows:
norm(arrayname, normorder=1);where arrayname is the name of the array whose L1 norm of the vector must be calculated and
normorder specifies the norm order of the vector, which is 1 for the L1 norm of a vector.
2. norm() function is used to calculate the L2 norm of the vector in NumPy using the formula:
and the syntax for the same is as follows:
norm(arrayname);where array name is the name of the array whose L2 norm of the vector must be calculated.
3. norm() function is used to calculate the maximum value of the vector in NumPy using the formula:
and the syntax for the same is as follows:
norm(arrayname, inf);where array name is the name of the array whose L2 norm of the vector must be calculated and inf represents infinity.
Working of NumPy norm
The error of a given model in machine learning and deep learning can be evaluated by using a function called norm which can be thought of as the length of the vector to map the vector to a given positive value.
The length of the vector can be calculated using three vector norms, namely vector L1 norm, vector L2 norm, and vector max norm,
The Vector L1 norm represents the L1 norm of the vector, which calculates the absolute vector values sum.
The Vector L2 norm represents the L2 norm of the vector, which calculates the squared vectored values sum and finds its square root.
The vector max norm is used to calculate the vector’s maximum value.
Examples of NumPy normGiven below are the examples of NumPy norm:
Example #1Python program to demonstrate NumPynorm function to calculate the L1 norm of the vector of the newly created array:
Code:
#importing the package numpy and importing the package for norm import numpy as nump from numpy.linalg import norm #Creating an array by making use of array function in NumPy and storing it in a variable called nameofthearray nameofthearray = nump.array([1,2,3,4]) #Displaying the elements of nameofthearray followed by one line space by making use of n print 'The elements of the given array are:' print nameofthearray print 'n' #using norm function of NumPy and passing the created array as the parameter to that function along with 1 to specify the order of norm to find the L1 norm of vector value and store it in a variable called L1norm L1norm = norm(nameofthearray,1) #Displaying the L1 norm of vector value stored in L1norm variable print 'The L1 norm of vector value is:' print L1normOutput:
The package for NumPy is imported in the above program, and the package for using norm is imported. Then an array is created using the array function in NumPy, and it is stored in the variable called the name of the array. Then the elements of the array name of the array are displayed. Then the norm() function in NumPy is used to find the L1 norm of a vector bypassing the name of the array and the order of the norm, which is 1 as the parameter to the norm() function, and the result returned is stored in a variable called L1norm which is printed as the output on the screen. Finally, the output is shown in the snapshot above.
Example #2Python program to demonstrate NumPy norm function to calculate the L2 norm of the vector of the newly created array:
#importing the package numpy and importing the package for norm import numpy as nump from numpy.linalg import norm #Creating an array by making use of array function in NumPy and storing it in a variable called nameofthearray nameofthearray = nump.array([1,2,3,4]) #Displaying the elements of nameofthearray followed by one line space by making use of n print 'The elements of the given array are:' print nameofthearray print 'n' #using norm function of NumPy and passing the created array as the parameter to that function to find the L2 norm of vector value and store it in a variable called L1norm L2norm = norm(nameofthearray) #Displaying the L2 norm of vector value stored in L2norm variable print 'The L2 norm of vector value is:' print L2normOutput:
The package for NumPy is imported in the above program, and the package for using norm is imported. Then an array is created using the array function in NumPy, and it is stored in the variable called nameofthearray. Then the elements of the array nameofthearray are displayed. Then the norm() function in NumPy is used to find the L2 norm of the vector bypassing the nameofthearray array as the parameter to the norm() function, and the result returned is stored in a variable called L2norm, which is printed as the output on the screen. The output is shown in the snapshot above.
Example #3Python program to demonstrate NumPy norm function to calculate the maximum value of the vector of the newly created array:
Code:
#importing the package numpy and importing the package for norm import numpy as nump from numpy.linalg import norm from numpy import inf #Creating an array by making use of array function in NumPy and storing it in a variable called nameofthearray nameofthearray = nump.array([1,2,3,4]) #Displaying the elements of nameofthearray followed by one line space by making use of n print 'The elements of the given array are:' print nameofthearray print 'n' #using norm function of NumPy and passing the created array as the parameter to that function to find the maximum value of vector and store it in a variable called mnorm mnorm = norm(nameofthearray,inf) #Displaying the maximum value of vector value stored in mnorm variable print 'The maximum value of vector is:' print mnormOutput:
The package for NumPy is imported in the above program, and the package for using norm is imported. Then an array is created using the array function in NumPy, and it is stored in the variable called nameofthearray. Then the elements of the array nameofthearray are displayed. Then the norm() function in NumPy is used to find the maximum value of vector bypassing the nameofthearray array as the parameter to the norm() function, and the result returned is stored in a variable called mnorm, which is printed as the output on the screen. Finally, the output is shown in the snapshot above.
Recommended ArticlesThis is a guide to the NumPy norm. Here we discuss the concept of NumPynorm function in Python through definition, syntax, and working through programming examples and their outputs. You may also have a look at the following articles to learn more –
Update the detailed information about Working Of Md5() Functions In Php With Examples on the Cattuongwedding.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!