Unknown Yesterday

Friday, January 9, 2026

The Trig I Never Knew

I learned geometry many decades ago. 

I learned the formulas for the trigonometric functions.

They never made sense to me.

I knew what a tangent was. Why was it also sin/cos?

What was a secant, anyway?

My first realization was π. I thought I knew π. I knew the formulas for the circumference and the area of a circle. I didn't know that the formula for circumference was actually the definition of π.

First came the desire to know the circumference of a circle given the diameter. Second came the realization that it was always the ratio 3.14159265... Third came the name for the ratio, π.

My next realizations were for the trig functions. Where did they originate?

They originated with triangles and the pythagorean theorem. If two sides of the triangle are known, then the third side is known. 

Some basic triangles were needed that could be scaled to other triangles. The basic framework was the unit circle. A circle whose center was the origin of the x-y coordinate system with radius 1. A radius of 1 is used to create simple formulas.

A tangent is a line that intersects the circle in exactly one point.

A secant is a line that intersects the circle in two points. Only the part of the secants that are in the first quadrant will be drawn here. 

Draw a secant with angle θ from the origin to a point outside the circle, in the first quadrant.

Drop a vertical line to the x-axis from the point where the secant intersects the circle.

Draw another secant from the origin to a point outside the circle with an angle 90-θ.

The angle will be called θ. The complimentary angle is 90-θ.

Create a vertical tangent line at x=1.



Trigonometric functions.

The point where the secant crosses the circle defines sin θ and cos θ.

All we really need is sin, since cos θ = sin (90 - θ)

The point where the secant crosses the tangent line is the definition of tan.

By using similar triangles, the ratio of the sin θ / cos θ is the same as the ratio of tan θ / 1.

tan θ = sin θ / cos θ

The intersection of the complimentary secant is not in the diagram, but it would have coordinates (sin θ, cos θ) and its intersection with the tangent line would be (1, cot θ). 

cot θ = cos θ / sin θ

The length of the secant line from the origin to the tangent line is the definition of sec.

Using the pythagorean theorem on the triangle in the circle gives the identity that sin² θ + cos² θ = 1.

Using the distance formula yields the formula for sec in terms of cos.

First, calculate the square of the sec.

sec² θ = (tan θ - 0)² + (1 - 0)² = tan² θ + 1 = sin² θ / cos ² θ + 1 = (sin² θ + cos² θ) / cos ² θ = 1 / cos ² θ

Then take the square root.

sec θ = 1 / cos θ

A similar calculation with the complement yields

csc θ = 1 / sin θ



Thursday, June 11, 2020

Initializing git repo and setting remote

I have been creating a lot of git repos recently. I am tired of looking up the same information again and again.

I have been creating a lot of Java projects as I experiment with Spring Boot. After creating a repo, I experiment with it a bit, then decide if I want to keep working in it.

The first step is to change to the project directory and and a .gitignore file. I have been using Java on a Mac and this is the file I use.

HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### VS Code ###
.vscode/
 
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Mac OS $
.DS_Store


Execute git init in the project folder. This creates the .git folder that is the local repo.

I am using GitHub to store remote copies of the repo. Create a new, empty repo and issue the commands:

git commit -m "first commit"
git remote add origin https://git-username@github.com/git-username/git-repo.git
git push -u origin master
 
After these commands, I can use git push to commit changes.

Monday, June 8, 2020

Creating webapp using Spring Boot and Maven

I created a base Spring Boot project to see all the dependencies I needed for a simple web application.

I created a simple Spring Boot project using the Initializr. It had no extra packages added.

I added the spring-boot-starter-web package, which included tomcat. Tomcat had type 'provided'. If I omitted the version, the war did not work when deployed, but worked locally.

I created a simple home controller using the Controller annotation and the GetMapping annotation.

The default view resolver is to the /src/main/resources/static folder without an extension. I created home.html in the static package and the page was found. Static is copied to WEB-INF/classes.

I wanted to deploy the app to an external tomcat installation. I extended the application class from SpringBootServletInitializer to create a war file and identify the starting application.

I added a view resolver and set the view class to JSTL. I had to add a dependency for jstl. I had to add a webapp folder for the JSPs, /src/main/webapp. The path is to the root of the web app. I did not want my JSPs in the classes folder.

Be sure that the path to the JSTL library is http and not https.

These are all the updated files to get it working:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }             
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
           return application.sources(DemoApplication.class);
        }
        
        @Bean
        public ViewResolver internalResourceViewResolver() {
            InternalResourceViewResolver bean = new InternalResourceViewResolver();
            bean.setViewClass(JstlView.class);
            bean.setPrefix("/WEB-INF/jsp/");
            bean.setSuffix(".jsp");
            return bean;
        }

}

pom.xml


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.0.RELEASE</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.35</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
index.jsp

<%-- 
    Document   : index
    Created on : Jun 8, 2020, 1:01:38 PM
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

    <c:choose>
        <c:when test="${not empty param.name}">
            <h1>Hello ${param.name}</h1>
        </c:when>
        <c:otherwise>
            <h1>Hello Stranger</h1>
        </c:otherwise>
    </c:choose>
    
</body>
</html>



Wednesday, June 3, 2020

HttpClient Daemon Threads

I have discovered that java.net.http.HttpClient.send leaves daemon threads that do not terminate when interrupted.

I am using
  • Maven 1.6
  • Java 1.14
  • NetBeans 11.3
When I make a simple HTTP request using HttpClient.send using the maven goal exec.java, the code runs, but pauses and I get these warnings after 15 seconds, and then the app stops:

thread Thread[HttpClient-1-Worker-0,5,org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava] was interrupted but is still alive after waiting at least 14997msecs
thread Thread[HttpClient-1-Worker-0,5,org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava] will linger despite being asked to die via interruption
thread Thread[HttpClient-1-Worker-1,5,org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava] will linger despite being asked to die via interruption
thread Thread[HttpClient-1-Worker-2,5,org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava] will linger despite being asked to die via interruption
NOTE: 3 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava,maxpri=10]


It is interesting that if I use the Play button in NetBeans to run the code, it works without any warnings. NetBeans uses exec:exec instead of exec:java. One difference between exec:java and exec:exec is how daemon threads are handled. With exec:java, after maven is done doing its work, maven sends an interrupt to all the daemon threads, hoping that they will terminate nicely. The exec:java goal also runs the code in the same VM as maven.

The exec:exec goal does not show the warning. I do not know the reason, but it could be that it terminates the threads unceremoniously or it waits for them to finish.

Some properties for the exec:java goal control how daemon threads are handled.
  • daemonThreadJoinTimeout - if I set this to 60 seconds or more, then the daemon threads terminate on there own. I assume that the TCP connection was closed by one side, so the threads terminate.
  • cleanupDaemonThreads - if this is set to false, then the threads are not interrupted and the timeout is not used. I assume that the threads are killed.
  • stopUnresponsiveDaemonThreads - this is a bad idea for this example. If I set this to true, the app will never end. As soon as one is killed, another pops up in its place. I assume this would continue for a minute in this case, waiting for the TCP connection to close.
Conclusion: I will use exec:exec. The downside of using exec:exec is that it creates a separate VM to run the java program. If that becomes a problem, I will set cleanUpDaemonThreads to false in the exec:java goal.

Friday, May 22, 2020

Struggling with Nested Tables and Unit Tests

I am using Spring Boot with Spring Data and attempting to write unit tests.

I have a three tables: Orders have more than one Taco. Tacos have more then one ingredient.

I am attempting to read an Order, with all its Tacos, and the Ingredients for each Taco.

In Order, tacos is a list. In Taco, ingredients is a list.

Here are some good references:
JPQL queries. https://www.objectdb.com/java/jpa/query/jpql/from
EntityGraphs: https://www.radcortez.com/jpa-entity-graphs/
Problems with EntityGraphs: https://stackoverflow.com/questions/31943989/spring-data-jpa-and-namedentitygraphs
JPA LifeCycle: https://www.javabullets.com/jpa-entity-lifecycle/
Baeldung EntityGraph example: https://www.baeldung.com/jpa-entity-graph
Removing old data from cache when testing: https://stackoverflow.com/questions/19057802/junit-hibernate-testing-lazy-associations-fetched
Transactional testing: https://www.marcobehler.com/2014/06/25/should-my-tests-be-transactional 

I have tried many ways to complete the task. The most fruitful was with EntityGraph.

When I attempted the query, I received a Hibernate multiple bag exception.

I researched many articles on the net. One solution is to use a set, but one site cautioned against that, since it must create a Cartesian product of all the tables.
https://vladmihalcea.com/hibernate-multiplebagfetchexception/

I tried to follow the advice, but my example is a little different. I believe the example uses the same approach as nested select statements. I cannot use that idea, since that example is accessing two collections from the same parent table. I want to get all tacos for an order, then get all ingredients for each of those tacos. I cannot perform a join on tacos.ingredients, since tacos is a list. I would have to join on the elements in the list.

I am beginning to think that the best solution is to change to sets instead of lists. Without it, I am forced to do a separate select to get the ingredients for each taco and then add those ingredients to the original result set of the tacos in the order.

Here is a new thought: read all the tacos with their ingredients. Somehow, organize these tacos into orders. Bad idea.

I imagine there is no other way than creating the Cartesian product. I will try with sets.

I attempted to work with sets. The changes were minimal, since many of the methods return Iterable.

I used an EntityGraph with a subgraph for ingredients, but it failed with a null pointer exception.

@NamedEntityGraph(
    name = "Taco_Order.detail",
    attributeNodes = {
        @NamedAttributeNode(value="tacos"
                            ,subgraph="tacos-subgraph"
        )
    }
    ,subgraphs = {
        @NamedSubgraph(
            name="tacos-subgraph",
            attributeNodes= {
                @NamedAttributeNode("ingredients")
            })
    }
    )

If I removed the subgraph, it worked, it even retrieved the ingredients. Confusing.

I removed the entity graph and debugged the code. The tacos were not loaded, as was expected.

I will add the entity graph again and see what happens. It may be a case of stale data in the session. I still got the ingredients. I had added an eager findById to taco, maybe that caused a problem.

I am stuck. I do not know why the ingredients are read.






Tuesday, May 5, 2020

Observations about CHtml::link and regular links

I am using Yii and have links to several controllers.

I had normal links in the page that worked when I proceeded from the base URL. They failed after I clicked the Home button in the menu.

After clicking Home, the URL was .../index.php/site/index, referencing the site controller.

When I click the normal link,

   
  • Unit


  • it tries to access .../index.php/site/Unit and fails.

    When I click the CHtml::link in any of these formats,

       

  •    

  •    


  • it tries to access .../index.php/Unit/index and succeeds.

    When I click the modified CHtml::link,

       


  • it tries to access .../index.php/site/Unit/index and fails.

    The only one that works properly is the CHtml::link with an array for the destination URL.

    The normal URLs add to the existing URL, without noticing that index.php is the actual page and the rest of the URL is path info, so they fail.

    Looking at normalizeUrl and createUrl explain the reasons for the CHtml::links.

    • normalizeUrl is only called if the parameter is an array
    • $this->getId() returns the name of the current controller
    • $this->getAction()->getId() returns the name of the action for the current controller
    • The remaining items are from createUrl:
      • If the route parameters is empty, return the current URL
      • If the route does not contain a /, then replace the current action with / and the route
      • If the leading character in the route is not / and the current module is not null, then replace the current controller with the module and add the route
      • If the route has a site/action or /site, then create the URL from the Yii app, not the current controller
    Conclusion, when creating a link for a different controller, use CHtml::link with an array parameter so that the URL is created based on the Yii app and not the current controller.



      Monday, May 4, 2020

      Yii CActiveRecord Relation to a Relation that is a View

      I have tables for Council, Unit, Offices, CouncilOffices, and ViewWithPhonebook. The view is the records for the unit with additional data retrieved from an online phonebook.

      CouncilOffices implements a many-to-many relationship between Offices and Council. The council are unit members who hold offices in the organization.

      Council Relations

      array(
      'officeRelation'=>array(self::MANY_MANY, 'Offices', 
                                    'council_offices(council_index, offices_index)'
      ), 
      'viewRelation'=>array(self::BELONGS_TO,
                  'ViewPhonebook', 'id'),

      'unitRelation'=>array(self::BELONGS_TO, 
                  'Unit', 'id'),       
      );

      CouncilOffices Relations

      array(
          'councilRelation'=>array(self::BELONGS_TO, 
                                   'Council', 'council_index'),
          'officesRelation'=>array(self::BELONGS_TO, 
                                   'Offices', 'offices_index'),
      );

      Offices Relations

      array(
           'councilRelation'=>array(self::MANY_MANY, 'Council',
                'council_offices(offices_index, council_index)',
            ),
      );

      Unit Relations

      array(
           'unitRelation'=> array(self::HAS_MANY, 'Council', 'id'),);

      ViewWithPhonebook Relations

      return array(
           'viewRelation'=> array(self::HAS_MANY, 'Council', 'id'),);

      The code worked if I used the unit relation. The problem I had was with the view, since it did not have a primary key. If I tried to include the relation, I received this SQL error.
      ON
      (`councilRelation`.`id`=`unitRelation`.``) WHERE (year=:year).
      It is clear that the generated SQL was looking for a primary key, but couldn't find it. I searched for a long time for a solution. I finally found the solution at http://codeinch.com/yii-set-primary-key-in-model/. Add a primaryKey method to the model and return the name of the primary key.

      public function primaryKey() {
          return 'ID';
      }

      Once this was set, I could retrieve the Council relation from Council Offices and then retrieve the View relation from the Council.

      $criteria = new CDbCriteria;
      $criteria->condition = 'year=:year';
      $criteria->params = array(':year'=>'2020');

      $data = CouncilOffices::model()
        ->with(array(
          'councilRelation'
             =>array('select'=>'index, year, id'),        
          'councilRelation.unitRelation'
             =>array('select'=>'ID, Name')))
        ->findAll($criteria);

      More Blogs

      Followers