# PHP & More

## A technical blog about programming in PHP and about technology in general: programming, workarounds and troubleshooting

Matplotlib is a great python library for plotting and graphics. Graphics include formatted text, text paths and tabular data. Mathematical expressions are a good reason to use Matplotlib for rendering text. Matplotlib also supports some widgets one can use for input. If you use Matplotlib widgets, you vetter know how to size and position them unless – for example – you write a program for yourself.

Following is an example of bad code:

from matplotlib import pyplot as plt
from matplotlib.widgets import TextBox
from matplotlib.widgets import Button

fig,ax=plt.subplots()

teextbox=TextBox(ax,"Label:")
button=Button(ax,'Submit')

plt.show()

The code above create to widgets that fill up the same plotting area inside defined Axes object. In the following image, you can see that both the text entered by the user and the button text overlap. In addition. the text entered by the user exceeds the limit of the plotting area.

## Get More Control over Your Widgets

For better results, there are 3 things to do:

• Use separate plotting area for your widgets.
• Set the plotting areas’ positions and sizes.
• Using Event Handlers to control the input length in a TextBox and perform operations.

### Separate Plotting Areas

The command plt.subplots() creates a figure, and a single plotting area, a uni-dimensional array of plotting areas or a bi-dimensional array of plotting areas. According to the number of rows and columns. The default is one row and one column. For example:

fig,ax=plt.subplots(nrows=2)

Returns a column of two plotting areas, To set the number of columns use the keyword argument ncols.

Let us see what happens if we set the number of colums (not adding widgets. yrt) by the following code:

from matplotlib import pyplot as plt
from matplotlib.widgets import TextBox
from matplotlib.widgets import Button

fig,ax=plt.subplots(nrows=2)

plt.show()

The code generates two Axes rectangles as follows:

### Resizing and Positioning a Plotting Area

The Axes rectangle can be resized and positioned using the function matplotlib.axes.Axes.set_position. One of its arguments can be an array whose members are left,bottom,width and height. The position and size is relative to the figure. :

• left=0 means that the Axes begin at the left side of the figure
• left=1 means that the Axes begin at the right side of the figure (which makes them invisible).
• bottom=0 means that the Axes begin at the bottom of the figure
• bottom=1 means that the Axes begin at the top of the figure

The following code resizes the Axes rectangles, and adds the widgets:

from matplotlib import pyplot as plt
from matplotlib.widgets import TextBox
from matplotlib.widgets import Button

fig,ax=plt.subplots(nrows=2)

ax[0].set_position([0.2,0.85,0.7,0.08])
ax[1].set_position([0.495,0.6,0.1,0.1])
button=Button(ax[1],'Submit')
plt.show()

ax[0] is the rectangle containing the TextBox

ax[1] is the rectangle containing the box. It’s width is 0.1(10% of that of the figure), and its left edge is position at 0.495, which is 0.5+0.1/2. This makes its horizontal alignment centered.

In the following image you’ll see that the background color of the text box is ‘cyan’, and hovercolor defines the background color when the mouse pointer is over the text box.

### Setting the Input Text’s Maximal Length and Event Handling

Event handling functions can be attached to widgets. Event handling functions can react to button clicks, text changes, submitions by pressing the Enter key, etc.

If you want to restrict the length of the input text in a TextBox, attach an event handling function as follows:

tb.on_text_change(func)

Where func is a function that gets one argument, the text. In this function, you can restrict the number of character. You better set the cursor position as well, because it increases whenever the user types a character, even if the text is changed by the event handler. Following is an example of how to check that the input matches a pattern all the way:

def tc_func(self,inp):
if (len(inp)&gt;self.maxlen):
self.tb.cursor_index=self.curpos
self.tb.set_val(self.val)
return
if (self.decpoint and inp.find('.')<0 and len(inp)&gt;self.maxlen-1):
self.tb.cursor_index=self.curpos
self.tb.set_val(self.val)
return
if (not self.pattern.match(inp)):
self.tb.cursor_index=self.curpos
self.tb.set_val(self.val)
return
self.val=inp
self.curpos=self.tb.cursor_index

From the argument self you can learn that the above function is a member of a class. Wrapping widgets in objects is recommended.

The member ‘cursor_index‘ is the position of the cursor after the event handler finishes its work. set_val sets a new value (or resets it).

The full source from which the code above is taken from my Square Root Calculator found at https://github.com/amity1/SquareRootCalculator

To handle button click events , use the function on_clicked, as follows:

button.on_clicked(click_handler)

The argument passed to the click_handler is an object of type matplotlib.backend_bases.MouseEvent. You can see in the following code how you can learn it:

from matplotlib import pyplot as plt
from matplotlib.widgets import TextBox
from matplotlib.widgets import Button

def click_handler(evt):
print(type(evt))
print("Button clicked with:"+ str(evt.button))

fig,ax=plt.subplots(nrows=2)

ax[0].set_position([0.2,0.85,0.7,0.08])
ax[1].set_position([0.495,0.6,0.1,0.1])
button=Button(ax[1],'Submit')
button.on_clicked(click_handler)
plt.show()

The event handler above prints the type of mits argument and the mouse button with which the button widget was clicked. Following is the output:

Button clicked with:MouseButton.LEFT

Button clicked with:MouseButton.MIDDLE

Button clicked with:MouseButton.RIGHT
<class 'matplotlib.backend_bases.MouseEvent'&gt;
Button clicked with:MouseButton.LEFT
<class 'matplotlib.backend_bases.MouseEvent'&gt;
Button clicked with:MouseButton.MIDDLE
<class 'matplotlib.backend_bases.MouseEvent'&gt;
Button clicked with:MouseButton.RIGHT

## Tables

A table is a widget that can be added to an Axes object in addition to other Artists.

There are two ways to create a table:

If you just choose to create a table without specifying loc, the table location in respect to the Axes, chances are you will not be satisfied.

The following code creates such a default table using the factory:

import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.widgets import TextBox
from matplotlib.widgets import Button

fig,ax=plt.subplots()
tab=mpl.table.table(ax,cellColours=[['red','green'],['yellow','blue']])

plt.show()

In the following image generated by the code, you will see that the table is created just under the Axes, and it overlaps the frame x-ticks.

You can create the table somewhere else by setting the loc parameter, you can set a cell’s width and height, set a column width automatically, and align text.

### Setting The Table’s Location and Modify Cells

To set a table location in respect to the Axes, pass the parameter loc with one of the valid codes, for example:

tab=mpl.table.table(ax,cellText=[['Red','Green'],['Yellow','Blue']],loc='upper left'

A default text alignment in a table cell can be defined by passing the parameter cellLoc when creating a new table. When adding a cell, the argument name is loc. The valid values for loc are: ‘left’, ‘center’ and ‘right’

Accessing a table cell is easy as ABC: access the cells as if the table were a bi-dimensional array whose elements are objects of type matplotlib.table.Cell. For example:

tab[row,col]

You can modify text properties using the function set_text_props of the cell object. And you can change its position and size by modifying properties inherited from class matplotlib.patches.Rectangle.

The following code creates a table near the upper left corner of the Axes, sets the column widths to be automatic, changes the color of text cells, and enlarges one of the cells.

import matplotlib as mpl
from matplotlib import pyplot as plt

fig,ax=plt.subplots()
tab=mpl.table.table(ax,cellText=[['Red','Green'],['Yellow','Blue']],
cellColours=[['red','green'],['yellow','blue']],
loc='upper left',cellLoc='left' )
tab.auto_set_column_width(0)
tab.auto_set_column_width(1)
tab[1,1].set_height(0.5)
for i,j in ((0,0),(0,1),(1,1)):
tab[i,j].set_text_props(color='white')

plt.show()

The code above produces the following image:

You can add single cells to a table using the function add_cell of the table.

the function should be called with the row number and column number. The caller has to specify the keyword arguments ‘width’ and ‘height’.

The new cell should be connected to the table, and may influence the heights and widths of celles in the same row or column.

The following code adds a cell in a new column and row:

import matplotlib as mpl
from matplotlib import pyplot as plt

fig,ax=plt.subplots()
tab=mpl.table.table(ax,cellText=[['Red','Green'],['Yellow','Blue']],
cellColours=[['red','green'],['yellow','blue']],
loc='upper left',cellLoc='left')
print ("Table Created")
tab.auto_set_column_width(0)
tab.auto_set_column_width(1)
tab[1,1].set_height(0.5)
for i,j in ((0,0),(0,1),(1,1)):
tab[i,j].set_text_props(color='white')
tab[1,0].set_xy((0,0.8))
plt.show()

In the following image, you can see a new orange cell that has been added to an existing table:

I recently installed VirtualBox configured internet connection to the internet for a virtual windows 7 machine I’ve installed using Vagrant, tried to setup USB, but got the following message: “Failed to save settings – Empty or null HostOnly interface name is not valid.”. Looking at the details, I saw the following “Result Code: E_FAIL (0x80004005) Component: NetworkAdapterWrapper …”.I don’t remember when I started to get that popup message. Couldn’t find any HostOnly adapter using the VirtualBox GUI. Finding the solution in the web is too hard. and I can’t find anything in the user guide either. But, one useful thing I know: in Linux and Unix-like system, I can find configuration files. They are usually found in directories whose names begin with ‘.’. followed by the program’s name or under “${HOME}/.config’ or directories created by the software. This time, the file was found in “${HOME}/VirtualBox VMs/<machine-name>”.
In this case: “${HOME}/VirtualBox VMs/windows7_default_1563689015423_90469/”. In this directory there are files with the suffix ‘.vbox’. They are XML files. In one of them, named “windows7_default_1563689015423_90469.vbox”, I find the following element: <Adapter slot="1" MACAddress="08002718154D" cable="true" type="82540EM"&gt; <DisabledModes&gt; <InternalNetwork name="intnet"/&gt; <NATNetwork name="NatNetwork"/&gt; </DisabledModes&gt; <HostOnlyInterface name="VirtualBox Host-Only Ethernet Adapter #2"/&gt; </Adapter&gt; Comment it out. Problem solved. HTML5 includes some features that allows developers to draw 3D shapes by drawing bi dimensional shapes and applying 3d transform, such as rotateX, rotateY and rotateZ. For convenience, you can shift the origin of axes using the style property If you don’t want the 3D image too flat (for example, all the faces of a cube having the same size) use perspective and perspective-origin style properties. You can use the matrix3d style properties instead of the named transforms if, for example, you don’t want to compute angles. ## The Style Properties A style property can be defined by adding the attribute style to an HTML element, defining a CSS class or accessing a DOM node. In this section I will explain the properties using a little Javascript program that draws a regular tetrahedron. Drawing a tetrahedron is done by drawing 4 isosceles triangles and rotating each of them once or twice. ### “perspective” and “perspective-origin” The distance and angle from which the shape is viewed. “perspective” holds the distance “perspective-origin” – a position value. For example: var main_div = d3.select('body') .append('div') .style('position','absolute') .style('top','50px') .style('left','50px') .style('perspective','50px') .style('perspective-origin','bottom left'); ### Transform Values: “rotateX”, “rotateY”, “rotate” and “transform-origin” Rotate an axis. Keep the value of the rotated axis coordinate unchanged, and change the rest. The axis is rotated around the position defined by “transform-origin” The following code adds the data for creating 4 triangles, and rotates 1 triangle 120 degrees to the right and 1 triangle 120 degrees to the left. Rotation is done around the bottom face’s centroid. main_div.selectAll('div'). data([{color: 'red', transform: null,upperVertexInd: true}, {color: 'black', transform: 'rotateX(90deg)', 'origin':' 100px 100px 0'}, {color: 'blue', transform: 'rotateY(120deg)',origin: cent roidString,upperVertexInd: true}, {color: 'green', transform: 'rotateY(-120deg)',origin: ce ntroidString,upperVertexInd: true}]) .enter() .append('div') .style('position','absolute') .style('top',0) .style('left',0) .style('transform',d=>d.transform) .style('transform-origin',d=>d.origin) .style('transform-style','preserve-3d') (To be more precise, it rotates the DIV elements) ### Tarnsform Values: “matrix3d” This matrix is used if you want to use a transformation out of the comfort zone. For example, a rotation transform with cosines and sines of the angle. The argument list contains 16 values, which are the cells of a square matrix of order 4 (4 rows and 4 columns). This matrix will be applied on (x,y,z,w) vector to get the target vector. When rotating a 2d vector )point), our original z-coordinate will be 0, and w will be 1. To specify the matrix: $\left( \begin{matrix} a_0 \ a_4 \ a_8 \ a_{12} \\ a_1 \ a_5 \ a_9 \ a_{13} \\ a_2 \ a_6 \ a_{10} a_{14} \\ a_3 \ a_7 \ a_{11} \ a_{15}\end{matrix} \right)$ use $matrix3d(a_0,a_1,a_2,...,a_{15})$ In my example, I will rotate 3 triangles, so their top vertex will go to a line perpendicular to the tetrahedron base, and passing through the base’s median. The median of a triangle is the point where median cross its other, dividing each median at the ratio 1:2. So, if each side of a triangle is of length 1. The height is $\sqrt(3)\over2$ Since, the height is the length of the median, the distance from a side to the centroid is the height divided by 3, and the requested sine is latex13latex 1 \over 3 The cosine is $\sqrt {1 - {1 \over 3}^2} = {\sqrt 8 \over 3}$ so, we will compute the matrix as follows: var rotateXCos = Math.sqrt(8) / 3; var rotateXSin = 1 / 3; var rotateXMat3d = [1,0,0,0, 0,rotateXCos,rotateXSin,0, 0,-rotateXSin,rotateXCos,0, 0,0,0,1]; var matrixTransformString = 'matrix3d(' + rotateXMat3d + ')'; Now, the code to draw the tetrahedron with *d3.js( is: var side=100; var len=100; var height=side * Math.sqrt(3)/2; var centroidZValue = -height / 3; // The point where medians meet. var rotateXCos = Math.sqrt(8) / 3; var rotateXSin = 1 / 3; var rotateXMat3d = [1,0,0,0, 0,rotateXCos,rotateXSin,0, 0,-rotateXSin,rotateXCos,0, 0,0,0,1]; var matrixTransformString = 'matrix3d(' + rotateXMat3d + ')'; var centroidString = '150px 0 ' + centroidZValue + 'px'; var main_div = d3.select('body') .append('div') .style('position','absolute') .style('top','50px') .style('left','50px') .style('perspective','50px') .style('perspective-origin','bottom left'); main_div.selectAll('div'). data([{color: 'red', transform: null,upperVertexInd: true}, {color: 'black', transform: 'rotateX(90deg)', 'origin':' 100px 100px 0'}, {color: 'blue', transform: 'rotateY(120deg)',origin: cent roidString,upperVertexInd: true}, {color: 'green', transform: 'rotateY(-120deg)',origin: ce ntroidString,upperVertexInd: true}]) .enter() .append('div') .style('position','absolute') .style('top',0) .style('left',0) .style('transform',d=>d.transform) .style('transform-origin',d=>d.origin) .style('transform-style','preserve-3d') .append('div') .style('transform-style','preserve-3d') .style('position','absolute') .style('top',0) .style('left',0) .style('transform',function(d){ return d.upperVertexInd?matrixTransformString:false; }) .style('transform-origin',function(d){ return d.upperVertexInd?'0 100px 0':false; }) .append('svg') .append('polygon') .attr('points',[100,100,150,100-height,200,100]) .style('fill','none') .style('stroke',d=>d.color); Some days ago I found that someone was looking for a D3.js expert. To prove one is an expert one has to pass a test, and one of the tasks on this test is to create a 3D color picker with RGB for axes. The cube faces cannot be filled with a bi-dimensional linear gradients because such gradients are not supported. So, you have to explicitly write a loop to add the pixels. Using SVG to add the pixels is a bad idea: SVG is an XML language, and uses a DOM tree. Using SVG will use a lot of memory and will slow down your computer. Use a canvas instead. Drawing on a canvas is done by simple Javascript commands, that add lines and shapes. Following is a little code snippet that fills a cube face: for (i=x1; i<=x2;i++){ for (j=y1; j<=y2; j++){ rgb_arr[d.rgb_variable[0]]=i; rgb_arr[d.rgb_variable[1]]=j; ctx.fillStyle=d3.rgb(rgb_arr[0],rgb_arr[1],rgb_arr[2]); ctx.fillRect(i,j,1,1); } } Now, to get the color where the mouse points, first get the position using the mouse event’s offsetX and offsetY. These properties will hold the correct value even if the canvas is rotated. Then you can get the RGBA values of the pixel using method getImageData of the canvas’ context. The method returns the data of a rectangle defined by 4 arguments: x,t,width and height. Following is an example: canvas.on('click', function(evt){ var ctx=d3.event.target.getContext('2d'); var pixelData = ctx.getImageData(d3.event.offsetX,d3.event.offsetY,1,1).data; alert(pixelData); }); The thought to create a copy of my Thunderbird profile did not cross my mind until the last power failure. After each power failure I found that I have to setup my mail account, and that all my events were “lost”. Wellm the events were not exactly lost, but a new calendar id was created, which is to be used to find all the events and their relevant properties in the calendar extension’s database. The database itself is an SQLite file, and in my system is located under ‘~/.thunderbird/default-dir/calendar-data/local.sqlite’ (Change the default-dir name to your local name, it is by default the one that ends with ‘.default*. ## Finding the Current Calendar Id The calendar id is the value of the Thunderbird’s user preference ‘calendar.list.sortOrder’. You can view this variables by choosing from the menu: preferences->preferences->preferences*, and then from the window opend, choosing the Advanced tab, and then clicking the “Config Editor* button. Continue at your own risk… Now, if you haven’t imported your calendar using Events And Tasks->Export. You can connect to the database using: sqlite3 /path/to/local.sqlite From now on, I’ll assume you only have one calendar. ## SQLite Tables The tables, indexes, trigges and other database entities are stored in a table named ‘sqlite_master’. To find tables, run the query select name from sqlite_master where type='table'; For each table column, run the query: pragma table_info(table_name); The result will be: cal_calendar_schema_version cal_attendees cal_recurrence cal_properties cal_events cal_todos cal_tz_version cal_metadata cal_alarms cal_relations cal_attachments Replace table_name by a table name, for example: pragma table_info(cal_events) The result will be: 0|cal_id|TEXT|0||0 1|id|TEXT|0||0 2|time_created|INTEGER|0||0 3|last_modified|INTEGER|0||0 4|title|TEXT|0||0 5|priority|INTEGER|0||0 6|privacy|TEXT|0||0 7|ical_status|TEXT|0||0 8|flags|INTEGER|0||0 9|event_start|INTEGER|0||0 10|event_end|INTEGER|0||0 11|event_stamp|INTEGER|0||0 12|event_start_tz|TEXT|0||0 13|event_end_tz|TEXT|0||0 14|recurrence_id|INTEGER|0||0 15|recurrence_id_tz|TEXT|0||0 16|alarm_last_ack|INTEGER|0||0 17|offline_journal|INTEGER|0||0 ## Finding the Last Calendar Id Now, I guess that an event in the last calendar has the latest event start date. A good query to find that event can be: select cal_id, title, event_start from cal_events order by event_start; Let us call the most recent value calendar id “old-cal-id”, and the new calendar id “new-cal-id* ## Updating and Deleting I suggest that you perform update and delete queries within transaction, so if something goes wrong you can rollback. Begin a transaction by running the command: begin transaction Now, for each table that has the column cal_id, run the query: update table_name set cal_id="new-cal-id" where cal_id="old-cal-id" Replace table_name by a name of a table that has the column cal_id, for example: update cal_event set cal_id="new-cal-id" where cal_id="old-cal-id"; Now, to delete the rest, run: delete from table_name where cal_id != "old-cal-id"; If everything’s fine, it’s time to commit your transaction by running: COMMIT; To learn more about SQLite, visit https://sqlite.org/index.html Matplotlib is a MATLAB-like library that allows Python programmers to create images and animations. For example, you can easily draw a graphic representation of functions with Y (and maybe Z) values generated by numpy and scipy functions. Matplotlib can also be interactive and handle events. The command mpl_connect is used for connecting an event with a callback function. ## The Backend Layer Someone on the IRC has challenged me with questions on how to perform some operations when the window is closed. In addition, I want the window title to be other than the default, “Figure 1”. Well, the layer that handles the main window is the backend layer, To find what backend Matplotlib uses, you can add the line print type(fig.canvas) The result may be something like: <class 'matplotlib.backends.backend_gtkagg.FigureCanvasGTKAgg'> This means that the backend used is ‘GtkAgg’. With the function ‘dir’, I’ve found that the canvass has a function named get_toplevel, and the returned value of fig.canvass.get_toplevel() is an object of type gtk.Window. This object has the methods of a GTK window. So you can change its title with the ‘set_titlemethod. For example: fig.canvas.get_toplevel().set_title(‘Rubic Cube’) You can tell your application what to do when the user closes the window, by calling its 'connect' method, with 'destroy' for first arguments. For example: fig.canvas.get_toplevel().connect(‘destroy’, destroyFunc, ‘Goodbye, cruel world!’) destroyFunc` is a function that accept 2 arguments (3 if a class member): the widget where the event has occurred and additional user defined data. More about Python FTK can be found at http://www.pygtk.org/pygtk2tutorial/index.html Last but not least, you can specify the backend Matplotlib will use, by calling the ‘use’ method of matplotlib. For example: matplotlib.use('GTKAgg') Note: This method should be called before importing ‘pyplot’. Written with StackEdit. The other day I downloaded “Fahrenheit 451” by Ray Bradbury as a free eBook, and tried to read it. I found that I cannot read much more than the titles of each chapter. I tried double-clicking and dragging, and saw that something appears on the screen, organized in paragraphs. Right Click->Inspect Element Now, a sub-window is opened at the bottom of the screen: You can see in that sub-window that some text in English appears in a ‘div’ element just under another ‘div’ element of class ‘textLayer’. To the right of the ‘Inspect Element’ sub-window you can see the CSS rules: As you can see, the value of property ‘color’ is ‘transparent’, You can edit that property using the color selector or by overriding the text value. Let’s set it to ‘black’. Now, you can see the paragraphs. You can change the opacity property of ‘div.textLayer’ from 0.2 to a higher value (up to 1) in order to read the text better. Firefox is a great PDF viewer, but my browser couldn’t save the document with the changes: the result was a corrupt file that cannot be opened. I’d written about it to the newsgroup ‘mozilla.wishlist’ found on server ‘news.mozilla.org’. and they opened a ticket. Before you upload an internet site, you better test it on your local machine. To do that, you should allocate an IP address known as a loopback address that does not require a modem for access. If you’ve installed Apache Httpd server, you’ll probably get an HTML page that reads “It works” upon connecting to “http://localhost” or http://127.0.0.1” from the web browser. But what if you want to create another site? How to make your server recognize an IP? In this post I will describe by example the process of adding a local IP. ## Step 1: Associate an IP with a Domain Name If you want to create a domain name such as ‘example.coq‘, add a line for it in /etc/hosts in the format: <inet-addr> <alias> For example: 127.0.0.2 example.coq ## Step 2: Attach the IP Address To a Network Interface To make an address available to internet servers, attach it to a network interface. A network interface is the identifier followed by colons at the beginning of blocks returned by the command ifconfigFor example: In this block lo0 is an interface name. lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384 options=600003<RXCSUM,TXCSUM,RXCSUM_IPV6,TXCSUM_IPV6> inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2 inet 127.0.0.1 netmask 0xff000000 From the prefix ‘lo’ of the interface name, you can know it is a loopback interface. You can attach an address, login as root, and run ifconfig like in the following example: ifconfig lo0 127.0.0.2 netmask 255.255.255.255 alias This will attach IP address 127.0.0.2 to the loopback interface ‘lo0’ You can make the operating system add it each type you start your computer, by adding the following line to /etc/rc.conf: ifconfig_lo0_alias0="inet 127.0.0.2 netmask 255.255.255.255" You can learn more about virtual hosts from the section ‘11.6 Virtual Hosts” of the FreeBSD Handbook ## Step 3: Start a Listening Server Now, you can start a server that will listen on your address. You can do it by adding a virtual host in apache httpd, create a server in ‘node.js’, etc. If you’ve installed ‘Apache24’ from the ports, you can find documentation in '/usr/local/share/doc/apache24'. In addition, you can find documentation in the httpd site. Written with StackEdit. One thing I wish to see in languages such as PHP is to find them supporting the complex type. Complex numbers are more than vectors in 2D, and I wish to see expression containing them parsed just like the ones with real numbers. Python supports them, and you have to import ‘cmath’ to use functions of a complex variable. To import cmath type import cmath For example, complex numbers are useful in solving cubic equations even if all its roots are real. And cubic equations can be used for Bézier curve manipulations. ## Following is the Cardan formula for solving a cubic equation Be $x^3 + ax^2 + bx + c=0$ a cubic equation. ### Step 1 Convert the equation to the form latex y^3 + py + q = 0 Use the Taylor series formula, to find a k, such that y=x-k: Be P(x) = $x^3 + ax^2 + bx + c$ Then, $P(x) = P(k) + P'(k)x + {P''(k)x^2 \over 2} + {P'''(k)x^3 \over 6}$ $P(k) = k^3 + ak^2 + bk + c$ $P'(k) = 3k^2 + 2ak + b$ $P''(k) = 6k + 2a$ $P'''(k) = 6$ Because P”(k)=0, 6k + 2a=0, thus: $k= - {a \over 3}$ . $p=P'(k) = b - {a^2 \over 3}$ $q=P(k) = {2a^3 \over 27} - {ba \over 3} + c$ For example, $x^3 - 7x^2 +14x - 8 = 0$ will become $y^3 -2{1 \over 3}y - {20 \over 27} = 0$ In Python: a = -7 b = 14 c = -8 p = b - a**2 / 3. q = 2*a**3 / 27. - b*a/3. - 8 ### Step 2 Find 2 numbers u and v that will help us solve the equation. If y=u+v , then the new equation will be: $u^3 + v^3 + (p + 3uv)(u + v) + q = 0$ We can find u,v such that (p + 3uv) = 0, Thus, and latex u^3 + v^3 = -q Since p+3uv=0, $u^3{v^3} = {-p^3 \over 27}$ From both equations, we get that latex u^3 and latex v^3 are the roots of the quadratic equations $t^2 +qt - {q^3 \over 27} = 0$ The roots of the quadratic equations are: (1) $u^3 = - {q \over 2} + \sqrt{{q^2 \over 4} + {p^3 \over 27}}$ (2) $v^3 = - {q \over 2} - \sqrt{{q^2 \over 4} + {p^3 \over 27}}$ In Python, the inner root can be computed using: innerRoot = cmath.sqrt(q**2 / 4. + p**3/27.) Now, u and v are cubic roots of (1) and (2) respectively. They must satisfy 3uv=-p. In Python, you get your initial u using: u=(-q / 2. + innerRoot) ** (1/3.) If the pair u,v does not satisfy 3uv = -p, you can multiply your v by$latex-1 + i \sqrt 3 \over 2 $until the pair satisfies the condition. Now, having a solution, get the next by multiplying u by$latex-1 + i \sqrt 3 \over 2 and v by latex-1 – i \sqrt 3 \over 2

In our example:
$u^3 = {20 \over 54} + \sqrt{{-263 \over 729}}$
$v^3 = {20 \over 54} - \sqrt{{-263 \over 729}}$

Let’s find our three solutions:
$u_1= (0.8333333333333335+0.28867513459481187j), v_1=(0.8333333333333335-0.28867513459481187j)$
Thus, latex $y_1 = (1.666666666666667+0j)$
$u_2 = (-0.6666666666666659+0.5773502691896264j), v_2=(-0.6666666666666657-0.5773502691896264j)$
Thus, $y_2 = (-1.3333333333333317+0j)$
$u_3 = (-0.1666666666666676-0.8660254037844383j), v_3=(-0.1666666666666677+0.866025403784438j)$
Thus, $y_3 = (-0.3333333333333353-2.220446049250313e-16j)$

(The above values are output from Python script. The real results look much better.)
Now, to get the roots of the original equation, add $k={-a \over 3}$ to each y.
In our example,
$k = 2{1 \over 3}$
Thus,
$x_1 = 4, x_2=1, x_3=2$

Writing expressions is much easier and more readable when the language supports the complex type.

When you see a change in the major version (the number before the first point of the version id), expect a great leap. New features have been added to PHP in version 7, that make programming more convenient. I’m going to discuss some of them.

## Null Coalescing

Suppose you’re trying to get a value from a request, and set it to zero if not sent. So instead of typing

$val =$_GET['foo'];
if ($val == null)$val=0;

Simply use ?? as follows:

$val =$_GET['foo'] ?? 0;

## The Spaceship Comparison Operator

When making a decision based on comparisons between to values, would you like to use a switch command instead of if ... else? Use the operator ‘<=>’ to compare numbers or strings.
$a<=>$b will return one of the following values:
* -1 if $a is smaller than$b
* 0 if $a equals$b
* 1 if $a is greater than$b

## Generator Functions

Generator functions have been introduced in PHP 5.5. They allow you to elegantly use generated values in a foreachloop without calling the function time and time again.
For example, the following code:

<?php
function factUpTo($n){$genValue=1;
for ($i=1;$i<=$n;$i++){
$genValue *=$i;
yield $genValue; } } foreach (factUpTo(8) as$j){
print $j . "\n"; } ?> produces the following output: 1 2 6 24 120 720 5040 40320 Following are features introduced in PHP 7: ### Returned Values In addition to generating values, a generator can return a value using the return command. This value will be retrieved by the caller using the method getReturn(). For example, the code: <?php$gen = (function() {
yield 1;
yield 2;

return 3;
})();

foreach ($gen as$val) {
echo $val, PHP_EOL; } echo$gen->getReturn(), PHP_EOL;

will produce the output:

1
2
3

### Delegation

A generator function can generate values using another generator function. This can be done by adding the keyword from after the command yield.
For example, the following code:

<?php
function gen()
{
yield 1;
yield 2;
yield 4;
yield from gen2();
}

function gen2()
{
yield 8;
yield 16;
}

foreach (gen() as $val) { echo$val, PHP_EOL;
}
?>

will produce the following output

1
2
4
8
16

## Return Type Declaration

A return type can be declare by adding it at the end of the function declaration after colons.
For example:

function myFunc(): int
{
.
.
.
return \$retValue;
}

returns an integer.

### Why Write The Return Type At The End?

Two possible reasons to write the type at the end of the declaration and not at the beginning like in Java, C, and other c-like languages:
* In PHP, the function declaration begins with the keyword funcction. The return type is optional.
* This syntax already exists in AcrionScript.

Find more about PHP 7 in the chapter Migrating from PHP 5.6.x to PHP 7.0.x of the php.net site documentation.

Written with StackEdit.